Skip to content

Commit

Permalink
add error message to Normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Sep 16, 2024
1 parent d4e72e3 commit 7eba995
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4626,14 +4626,22 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
normFunction = ast.arg2();
}
IExpr arg1 = ast.arg1();
if (arg1.isEmptyList() && ast.isAST1()) {
if (arg1.isEmptyList()) {
return arg1;
}
IExpr norm = engine.evaluate(F.unaryAST1(normFunction, ast.arg1()));
if (norm.isZero()) {
return arg1;
if (ast.isAST1() && arg1.isMatrix(false) != null) {
// The first argument is not a number or a vector, or the second argument is not a norm
// function that always returns a non-negative real number for any numeric argument.
return Errors.printMessage(S.Normalize, "nlnmt2", ast, engine);
}
if (ast.isAST2() || arg1.isNumber() || arg1.isVector() > 0) {
IExpr norm = engine.evaluate(F.unaryAST1(normFunction, arg1));
if (norm.isZero()) {
return arg1;
}
return F.Divide(arg1, norm);
}
return F.Divide(ast.arg1(), norm);
return F.NIL;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ public static void initGeneralMessages() {
"ncvi", "NIntegrate failed to converge after `1` refinements in `2` in the region `3`.", //
"ndimv", "There is no `1`-dimensional `2` for the `3`-dimensional vector `4`.", //
"nliter", "Non-list iterator `1` at position `2` does not evaluate to a real numeric value.", //
"nlnmt2",
"The first argument is not a number or a vector, or the second argument is not a norm function that always returns a non-negative real number for any numeric argument.", // ",
"nil", "unexpected NIL expression encountered.", //
"ninv", "`1` is not invertible modulo `2`.", //
"nmet", "Unable to find the domain with the available methods.", //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15818,6 +15818,14 @@ public void testNormal() {

@Test
public void testNormalize() {
// message Normalize: The first argument is not a number or a vector, or the second argument is
// not a norm function that always returns a non-negative real number for any numeric argument.
check("Normalize({{1, 2}, {4, 5}})", //
"Normalize({{1,2},{4,5}})");
// set the name of the norm to get a result
check("Normalize({{1, 2}, {4, 5}}, Norm)", //
"{{0.147758,0.295516},{0.591031,0.738789}}");

check(
"Normalize({0.9999999999999999, -0.12609662354252538+I*0.3870962681438435, 0.3692814336284687+I*0.0968290630091466})", //
"{0.8732080940136610352,-0.1101085923051267364+I*0.33801559450568667,0.3224595368133474565+I*0.0845519215553456008}");
Expand All @@ -15836,11 +15844,11 @@ public void testNormalize() {
"{1/Sqrt(91),(I*2)/Sqrt(91),3/Sqrt(91),(I*4)/Sqrt(91),5/Sqrt(91),(I*6)/Sqrt(91)}");
check("Normalize(N({1, 2*I, 3, 4*I, 5, 6*I}))", //
"{0.104828,I*0.209657,0.314485,I*0.419314,0.524142,I*0.628971}");
check("Normalize({{1, 2}, {4, 5}}, Norm)", //
"{{0.147758,0.295516},{0.591031,0.738789}}");

check("Normalize(1 + x + x^2, Integrate(#^2, {x, -1, 1}) &)", //
"5/22*(1+x+x^2)");

check("Normalize(1 + x + x^2)", //
"Normalize(1+x+x^2)");
check("Normalize({1, 1, 1, 1})", //
"{1/2,1/2,1/2,1/2}");
check("Normalize(1 + I)", //
Expand Down Expand Up @@ -19908,8 +19916,18 @@ public void testRational() {
"f(7/22,64/201,x/y)");
}

@Test
public void testRationalizeIssue1065() {
// issue 1065
checkNumeric("Rationalize(878159.58,1*10^-12) - Rationalize(431874.32,1*10^-12)", //
"22314263/50");
checkNumeric("22314263/50 // N", //
"446285.26");
}

@Test
public void testRationalize() {

// check("Rationalize(0.1234567*^2)", //
// "1234567/100000");
// // check("Rationalize(0.12345678*^2)", //
Expand Down

0 comments on commit 7eba995

Please sign in to comment.