I'm using Maxima to print stuff into a web page in an educational context. Most of the time everything works smoothly, but I'm having problems with binomials such as -x + 1, which Maxima simplifies as 1 - x. I quess that this is because this form takes less characters.
But in educational context this is often not desired, as in the standard form the term with x should always be before constant.
So is there any option to prevent this kind of simplification?
I have tried using ratvars(x), totaldisrep(-x+1) and declare(x,mainvar), none of those did what I was looking for.
Outputs:
x + 1;
x+1
x - 1;
x-1
-x - 1;
-x-1
-x + 1;
1-x
I wish to find a way to get this last one to output -x+1.
Try setting negsumdispflag to false. See: ? negsumdispflag
I get these results, maybe this is acceptable.
(%i12) negsumdispflag:false;
(%o12) false
(%i13) x + 1;
(%o13) x + 1
(%i14) x - 1;
(%o14) x - 1
(%i15) 1 - x;
(%o15) (- x) + 1
(%i16) - 1 - x;
(%o16) (- x) - 1
Related
Is there a function which rounds numbers (even decimal numbers) like round() in Excel?
Example
Round 1,45 to one decimal: 1,5
Round 2,45 to one decimal: 2,5
There is a similar question but they use a different algorithm.
OK, here's an attempt to reimplement Excel =ROUND function in Maxima. Some notes. (1) Values are rounded to 15 significant digits before applying the user's rounding. This is an attempt to work around problems caused by inexact representation of decimals as floating point numbers. (2) I've implemented excel_round and integer_log10 as so-called simplifying functions. That means that the calculation isn't carried out until the arguments are something that can be evaluated (in this case, when the arguments are numbers). (3) I didn't check to see what Excel =ROUND does with negative numbers -- does it round 5 upward (i.e., towards zero in this case), or away from zero? I dunno.
I've posted this solution as the little package excel_round.mac on Github. See: https://github.com/maxima-project-on-github/maxima-packages and navigate to robert-dodier/excel_round. In the interest of completeness, I've pasted the code here as well.
Here are a few examples.
(%i1) excel_round (1.15, 1);
(%o1) 1.2
(%i2) excel_round (1.25, 1);
(%o2) 1.3
(%i3) excel_round (12.455, 2);
(%o3) 12.46
(%i4) excel_round (x, 2);
(%o4) excel_round(x, 2)
(%i5) ev (%, x = 9.865);
(%o5) 9.87
Here is the code. This is the content of excel_round.mac.
/* excel_round -- round to specified number of decimal places,
* rounding termminal 5 upwards, as in MS Excel, apparently.
* Inspired by: https://stackoverflow.com/q/62533742/871096
*
* copyright 2020 by Robert Dodier
* I release this work under terms of the GNU General Public License.
*/
matchdeclare (xx, numberp);
matchdeclare (nn, integerp);
tellsimpafter (excel_round (xx, nn), excel_round_numerical (xx, nn));
matchdeclare (xx, lambda ([e], block ([v: ev (e, numer)], numberp(v))));
tellsimpafter (excel_round (xx, nn), excel_round_numerical (ev (xx, numer), nn));
excel_round_numerical (x, n) :=
block ([r, r1, r2, l],
/* rationalize returns exact rational equivalent of float */
r: rationalize (x),
/* First round to 15 significant decimal places.
* This is a heuristic to recover what a user "meant"
* to type in, since many decimal numbers are not
* exactly representable as floats.
*/
l: integer_log10 (abs (r)),
r1: round (r*10^(15 - l)),
/* Now begin rounding to n places. */
r2: r1/10^((15 - l) - n),
/* If terminal digit is 5, then r2 is integer + 1/2.
* If that's the case, round upwards and rescale,
* otherwise, terminal digit is something other than 5,
* round to nearest integer and rescale.
*/
if equal (r2 - floor(r2), 1/2)
then ceiling(r2)/10.0^n
else round(r2)/10.0^n);
matchdeclare (xx, lambda ([e], numberp(e) and e > 0));
tellsimpafter (integer_log10 (xx), integer_log10_numerical (xx));
matchdeclare (xx, lambda ([e], block ([v: ev (e, numer)], numberp(v) and v > 0)));
tellsimpafter (integer_log10 (xx), integer_log10_numerical (ev (xx, numer)));
matchdeclare (xx, lambda ([e], not atom(e) and op(e) = "/" and numberp (denom (e)) and pow10p (denom (e))));
pow10p (e) := integerp(e) and e > 1 and (e = 10 or pow10p (e/10));
tellsimpafter (integer_log10 (xx), integer_log10 (num (xx)) - integer_log10_numerical (denom (xx)));
integer_log10_numerical (x) :=
if x >= 10
then (for i from 0 do
if x >= 10 then x:x/10 else return(i))
elseif x < 1
then (for i from 0 do
if x < 1 then x:x*10 else return(-i))
else 0;
The problem of rounding numbers is actually pretty subtle, but here is a simple approach which I think gives workable results. Here I define a new function myround which has the behavior described for Excel =ROUND. [1]
(%i4) myround (x, n) := round(x*10^n)/10.0^n;
n
'round(x 10 )
(%o4) myround(x, n) := -------------
n
10.0
(%i5) myround (2.15, 1);
(%o5) 2.2
(%i6) myround (2.149, 1);
(%o6) 2.1
(%i7) myround (-1.475, 2);
(%o7) - 1.48
(%i8) myround (21.5, -1);
(%o8) 20.0
(%i9) myround (626.3, -3);
(%o9) 1000.0
(%i10) myround (1.98, -1);
(%o10) 0.0
(%i11) myround (-50.55, -2);
(%o11) - 100.0
[1] https://support.microsoft.com/en-us/office/round-function-c018c5d8-40fb-4053-90b1-b3e7f61a213c
We have an online platform where we use maxima on the backend to give feedback on the answers that the student give.
Say that the solution of a polynomial long division is the following function:
sol: 3+(2+4*x)/(3*x^2+2*x+8);
Let's say that the student gives the answer:
ans: 3 +(3-5*x)/(3x^2+2*x+8)
We then want to give feedback along the lines.
Your answer is indeed of the following form:
The constant s is correct, but you didn't choose the right linear function r(x).
I try to implement this feedback in the following way:
ans: 3 +(3-5*x)/(3x^2+2*x+8);
remvalue(a,b,c,d);
matchdeclare([a,b,c,d], constantp);
defmatch(match, d+ (a*x+b)/(3*x^2+2*x+8));
match(ans);
However, defmatch doesn't seem to be able to match this. Is there any other function I can use for matching quotient function like this?
It does work in more simple scenarios:
ans: (3-5*x)/(3*x^2+2*x+8);
remvalue(a,b,c);
matchdeclare([a,b,c], constantp);
defmatch(match, (a*x+b)/(3*x^2+2*x+8));
match(ans);
If the problems are all polynomial division, maybe you can use the divide function which returns the quotient and remainder and then verify that the student's input is the same as quotient + remainder/divisor.
As to matching quotient + remainder/divisor, defmatch might not be powerful enough because it is mostly (although not entirely) looking for formal matches, and it can't, in many cases, detect expressions which are equivalent but formally different.
Let me think about how to implement a match function for this problem. Perhaps others have a suggestion too.
EDIT: Here's a way to match such expressions. I'll separate a ratio of polynomials from other terms. If there is one ratio term, and all the other terms are a polynomial, then the pattern you want is matched.
I've made use of at least one obscure but useful and even comprehensible feature of matching of "+" expressions. Since "+" is commutative, the pattern matcher sweeps up all the arguments which match the first predicate that is attempted, then sweeps through with the second, and so, if there are more than two predicts. If any pattern matching variable is declared to match all, it matches all arguments and there are none left. The pattern variables will be attempted in reverse order, so aa will be processed last. The effect is to divide a "+" expression into terms which match a stated predicate and a catch-all for everything else. This is made use of in %i7.
(%i2) matchdeclare (pp, lambda ([e], polynomialp (e, [pvar]))) $
(%i3) matchdeclare (qq, lambda ([e], diff (e, pvar) # 0 and polynomialp (e, [pvar]))) $
(%i4) defmatch (match_pratio, pp/qq, pvar) $
(%i5) matchdeclare (aa, all) $
(%i6) matchdeclare (rr, lambda ([e], match_pratio (e, pvar) # false)) $
(%i7) defmatch (match_pratio_plus_remainder, rr + aa, pvar) $
(%i8) match_foo (e, pvar) :=
block ([aa, pp, qq, rr],
if match_pratio (e, pvar) # false
then [0, pp, qq, pp/qq]
elseif match_pratio_plus_remainder (e, pvar) # false
then if polynomialp (aa, [pvar]) and op(rr) # "+"
then [aa, pp, qq, rr]) $
Here are some examples that match:
(%i9) match_foo (u^2/2 - 3*u + 1 + (u - 1)/(u^2 + u - 1), u);
2
u 2 u - 1
(%o9) [-- - 3 u + 1, u - 1, u + u - 1, ----------]
2 2
u + u - 1
(%i10) match_foo (u^2/2 - 3*u + 1 - 2*(u - 1)/(u^2 + u - 1), u);
2
u 2 2 (u - 1)
(%o10) [-- - 3 u + 1, - 2 (u - 1), u + u - 1, - ----------]
2 2
u + u - 1
(%i11) match_foo (u^2/2 - 3*u + 1 - 2/(u^2 + u - 1), u);
2
u 2 2
(%o11) [-- - 3 u + 1, - 2, u + u - 1, - ----------]
2 2
u + u - 1
(%i12) match_foo (1 - 2/(u^2 + u - 1), u);
2 2
(%o12) [1, - 2, u + u - 1, - ----------]
2
u + u - 1
(%i13) match_foo (- 2/(u^2 + u - 1), u);
2 2
(%o13) [0, - 2, u + u - 1, - ----------]
2
u + u - 1
Here are some examples that don't match: two polynomial ratio terms, no polynomial ratio, and a nonpolynomial term.
(%i14) match_foo (1/u - 2/(u^2 + u - 1), u);
(%o14) false
(%i15) match_foo (1 - (u^2 + u - 1)/2, u);
(%o15) false
(%i16) match_foo (sin(u) - 2/(u^2 + u - 1), u);
(%o16) false
SECOND EDIT: I think I've misnamed match_pratio_plus_remainder, it should probably be match_remainder_plus_quotient. Oh well.
The following integral does is not evaluated by Maxima:
integrate(charfun(x<1/2), x, 0, 1);
Is there a different trick to make it work, or is it simply un-implemented?
The share package abs_integrate can integrate some expressions containing signum, abs, and unit_step. In this case you can write charfun(x < 1/2) in terms of signum(1/2 - x) and then abs_integrate can handle it.
You'll need to load abs_integrate. Note that abs_integrate modifies the behavior of integrate; there isn't a separate abs_integrate function to call.
(%i2) load (abs_integrate) $
(%i3) integrate (signum (1/2 - x), x, 0, 1);
(%o3) 0
(%i4) integrate (signum (1/2 - x), x, -1, 1);
(%o4) 1
(%i5) foo (e) := (1 + signum(e))/2;
1 + signum(e)
(%o5) foo(e) := -------------
2
(%i6) integrate (foo (1/2 - x), x, 0, 1);
1
(%o6) -
2
(%i7) integrate (foo (1/2 - x), x, -1, 1);
3
(%o7) -
2
Note that foo corresponds to charfun here.
I am current determined to complete a problem in Lua and have no idea where to begin. I was thinking about beginning with a modulus operator. I am searching for advice from experienced Lua programmers on how to program this and mainly how I can calculate the theoretically mathematical side of the problem.
Source of the question... (http://www.eecs.qmul.ac.uk/~pbo/ACM/archive/00209.html)
Gratitude will be shown to anyone who answers correctly.
Thank-you.
function get_left(max)
i = 0
j = 1
ls = {}
repeat
i = i + 1
j = j + i - 1
ls[i] = j
print (ls[i], " ls")
until j >= max
return ls
end
a = get_left(27)
(need to format that as code -.-)
if the point1 is between a[i] and a[i+1]
if then point2 is still between a[i] and a[i+1] it is on the same line
else if point2 is between a[i + n] and a[i + n + 1]
then if point2 is at a[i + n] + (a[i] - point1) + n + 1 its in a strait line right above it
else if point2 is at a[i + n] + (a[i] - point1) + n - 1 its in a strait line left above it
if for all points n or n*(-1) is equal the distance between the points is equal.
That is if I didn't make any logical errors and you probably have to check more for it to work properly.
This is more a mathematical question than lua I recommend adding a math tag to it.
In the Maxima session below, how come f(1) is not 0?
(%i1) eq: 2 * x + 1 = 3;
(%o1) 2 x + 1 = 3
(%i2) f(x) := lhs(eq) - rhs(eq);
(%o2) f(x) := lhs(eq) - rhs(eq)
(%i3) f(1);
(%o3) 2 x - 2
the process of function calling in maxima here binds x to 1 in the function
definition, lhs(eq)-rhs(eq). That has no x in it, so that binding does nothing.
Next, lhs(eq) is evaluated to 2*x+1. rhs(eq) is evaluated to 3. etc.
Do you always want the same equation eq? perhaps you want to do
define(f(x),lhs(eq)-rhs(eq));
to check what the definition is, try
grind(f);
If you want to vary the equation maybe something like
g(val, eq) := subst(val,x, lhs(eq)-rhs(eq)) ;
would do.