I'm using wxMaxima 15.08.1 (win 10) and when I input this equation
/* [wxMaxima: input start ] */a*x+b*y+c*z=0;
I get this:
/* [wxMaxima: input end ] */cz+by+ax=0
Why does it change the term's position of the expression? It seems like in descending order somehow.
Then, if I type another equation giving all coefficients the same unknown, maxima outputs it just right.
/* [wxMaxima: input start ] */a*x^2+b*x+c=0;
/* [wxMaxima: input end ] */ax^2+bx+x=0
Maxima has its own idea of the canonical ordering of terms in "+" and "*" expressions. The canonical ordering is expressed by the function ordergreatp (equivalently orderlessp) which tells if one term comes after (respectively, before) another term. If you apply sort to a list of terms, they are sorted, by default, according to the canonical order.
By default, "+" terms are displayed in reverse order (reverse of the canonical order). When the global variable powerdisp is true, "+" terms are displayed in the canonical order. You can decide whether one order or the other works better for you.
(%i2) powerdisp;
(%o2) false
(%i3) a*x + b*y + c*z;
(%o3) c z + b y + a x
(%i4) a*x^2 + b*x + c;
2
(%o4) a x + b x + c
(%i7) powerdisp : true $
(%i8) a*x + b*y + c*z;
(%o8) a x + b y + c z
(%i9) a*x^2 + b*x + c;
2
(%o9) c + b x + a x
Related
Consider the following scenario, for A with size [k, 1, m] and B with size [1, n, m], how can one get the same result as:
C = sum(A .* B, 3);
without expanding
A .* B
Because that takes way too much memory.
Something like the following loop but natively:
C = zeros(k,n);
for idx = 1:m
C += A(:,1,idx) * B(1,:,idx);
end
I guess I could also ask if there's a function like bsxfun with a "reduce"-like behavior?
Something like:
C = bsxfun_accumulate(#(a, b) a * b, A, B);
Note: by native I mean cs/cuda code-paths, or opencl code-path, or x86-sse, or plain x86 instructions. Whatever is available.
You can actually solve your problem by simply reshaping the variables A and B and applying a matrix multiply:
C = reshape(A, [], m)*(reshape(B, [], m).');
Basically, summing the results of m sets of multiplications involving k-by-1 column vectors and 1-by-n row vectors is the equivalent of multiplying a k-by-m matrix of your columns and an m-by-n matrix of your rows.
I'm creating a game for kids. It's creating a triangle using 3 lines. How I approached this is I create two arcs(semi circle) from two end points of a base line. But I couldn't figure how to find the point of intersection of those two arc. I've search about it but only found point of intersection between two straight lines. Is there any method to find this point of intersection? Below is the figure of two arcs drawn from each end of the baseline.
Assume centers of the circle are (x1, y1) and (x2, y2), radii are R1 and R2. Let the ends of the base be A and B and the target point be T. We know that AT = R1 and BT = R2. IMHO the simplest trick to find T is to notice that difference of the squares of the distances is a known constant (R1^2 - R2^2). And it is easy to see that the line the contains points meeting this condition is actually a straight line perpendicular to the base. Circles equations:
(x - x1)^2 + (y-y1)^2 = R1^2
(x - x2)^2 + (y-y2)^2 = R2^2
If we subtract one from another we'll get:
(x2 - x1)(2*x - x1 - x2) + (y2 - y1)(2*y - y1 - y2) = R1^2 - R2^2
Let's x0 = (x1 + x2)/2 and y0 = (y1 + y2)/2 - the coordinates of the center. Let also the length of the base be L and its projections dx = x2 - x1 and dy = y2 - y1 (i.e. L^2 = dx^2 + dy^2). And let's Q = R1^2 - R2^2 So we can see that
2 * (dx * (x-x0) + dy*(y-y0)) = Q
So the line for all (x,y) pairs with R1^2 - R2^2 = Q = const is a straight line orthogonal to the base (because coefficients are exactly dx and dy).
Let's find the point C on the base that is the intersection with that line. It is easy - it splits the base so that difference of the squares of the lengths is Q. It is easy to find out that it is the point on a distance L/2 + Q/(2*L) from A and L/2 - Q/(2*L) from B. So now we can find that
TC^2 = R1^2 - (L/2 + Q/(2*L))^2
Substituting back Q and simplifying a bit we can find that
TC^2 = (2*L^2*R1^2 + 2*L^2*R2^2 + 2*R1^2*R2^2 - L^4 - R1^4 - R2^4) / (4*L^2)
So let's
a = (R1^2 - R2^2)/(2*L)
b = sqrt(2*L^2*R1^2 + 2*L^2*R2^2 + 2*R1^2*R2^2 - L^4 - R1^4 - R2^4) / (2*L)
Note that formula for b can also be written in a different form:
b = sqrt[(R1+R2+L)*(-R1+R2+L)*(R1-R2+L)*(R1+R2-L)] / (2*L)
which looks quite similar to the Heron's formula. And this is not a surprise because b is effectively the length of the height to the base AB from T in the triangle ABT so its length is 2*S/L where S is the area of the triangle. And the triangle ABT obviously has sides of lengths L, R1 and R2 respectively.
To find the target T we need to move a along the base and b in a perpendicular direction. So coordinates of T calculated from the middle of the segment are:
Xt = x0 + a * dx/L ± b * dy / L
Yt = y0 + a * dy/L ± b * dx / L
Here ± means that there are two solutions: one on either side of the base line.
Partial case: if R1 = R2 = R, then a = 0 and b = sqrt(R^2 - (L/2)^2) which makes obvious sense: T lies on the segment bisector on a length of sqrt(R^2 - (L/2)^2) from the middle of the segment.
Hope this helps.
While you have not stated clearly, I assume that you have points with coordinates (A.X, A.Y) and (B.X, B.Y) and lengths of two sides LenA and LenB and need to find coordinates of point C.
So you can make equation system exploiting circle equation:
(C.X - A.X)^2 + (C.Y - A.Y)^2 = LenA^2
(C.X - B.X)^2 + (C.Y - B.Y)^2 = LenB^2
and solve it for unknowns C.X, C.Y.
Not that it is worth to subtract A coordinates from all others, make and solve simpler system (the first equation becomes C'.X^2 + C'.Y^2 = LenA^2), then add A coordinates again
So I actually needed this to design a hopper to lift grapes during the wine harvest. Tried to work it out myself but the algebra is horrible, so I had a look on the web -in the end I did it myself but introduced some intermediate variables (that I calculate in Excel - this should also work for the OP since the goal was a calculated solution). In fairness this is really much the same as previous solutions but hopefully a little clearer.
Problem:
What are the coordinates of a point P(Xp,Yp) distance Lq from point Q(Xq,Yq) and distance Lr from point R(Xr,Yr)?
Let us first map the problem onto to new coordinate system where Lq is the origin, thus Q’ = (0,0), let (x,y) = P’(Xp-Xq,Yp-Yq) and let (a,b) = R’(Xr-Xq,Yr-Yq).
We may now write:
x^2 + y^2 = Lq^2 -(1)
(x-a)^2 + (y-b)^2 = Lr^2 -(2)
Expanding 2:
x^2 – 2ax + a^2 + y^2 -2ay + b^2 =Lr^2
Subtracting 1 and rearranging
2by = -2ax + a2 + b2 - Lr^2+ Lq^2
For convenience, let c = a^2 + b^2 + Lq^2 + Lr^2 (these are all known constants so c may be easily computed), thus we obtain:
y = -ax/b + c/2b
Substituting into 1 we obtain:
x^2 + (-a/b x + c/2b)^2 = Lq^2
Multiply the entire equation by b^2 and gather terms:
(a^2 + b^2) x2 -ac x + c/4 + Lq^2 b^2 = 0
Let A = (a2 + b2), B= -ac ,and C= c/4 + Lq^2 b^2
Use the general solution for a quadratic
x = (-B +-SQRT(B^2-4AC))/2A
Substitute back into 1 to get:
y= SQRT(Lq^2 - x^2 )
(This avoids computational difficulties where b = 0)
Map back to original coordinate system
P = (x+Xq, y + Yq)
Hope this helps, sorry about the formatting, I had this all pretty in Word, but lost it
maxima accepts both a^b and a**b as input for exponentiation, and will always output the exponent with caret ^.
Is it also possible to get the output as a function, like pow(a,b)?
OK, as you said, you want to output Math.pow(a,b) for Javascript. The approach I'll suggest here is to replace a^b expressions in Maxima with Math.pow(a,b) expressions and output that.
(%i1) e : sqrt(a) + b^(3/2) + 1/c + exp(d^f);
f
d 1 3/2
(%o1) %e + - + b + sqrt(a)
c
(%i2) subst ("^"=lambda([a, b], Math.pow(a, b)), e);
3 1
(%o2) Math . pow(c, - 1) + Math . pow(b, -) + Math . pow(a, -)
2 2
+ Math . pow(%e, Math . pow(d, f))
OK, so that's most of the work there. Some expressions are represented as "^" expressions even if they appear to be something else, for example, sqrt(a) is a^(1/2) and 1/c is c^(-1). If you need for those to be preserved as sqrt(a) and 1/c then we'll have to work on that.
I'm guessing it's best to have floating point values instead of integer ratios. Also, we'll replace %e by its numerical value. If you want %e^x to be rendered as Math.exp(x), we can work on that. Or if you want Math.pow(Math.E, x), that's relatively simple; just evaluate subst(%e = Math.E, <your expression>).
(%i3) float (%);
(%o3) Math . pow(c, - 1.0) + Math . pow(b, 1.5) + Math . pow(a, 0.5)
+ Math . pow(2.718281828459045, Math . pow(d, f))
Maxima considers x . y to mean noncommutative multiplication, but that doesn't come into play here so that's fine. By default it is displayed with a space on either side of the dot, but if you're willing to do a tiny amount of Lisp hacking we can remove the space. (I guess it doesn't matter to Javascript, right? Math . pow is equivalent to Math.pow, isn't it?)
(%i4) :lisp (setf (get 'mnctimes 'dissym) '(#\.))
(.)
(%i4) %o3;
(%o4) Math.pow(c, - 1.0) + Math.pow(b, 1.5) + Math.pow(a, 0.5)
+ Math.pow(2.718281828459045, Math.pow(d, f))
OK, now we can output the expression.
(%i5) grind (%o3);
Math.pow(c,-1.0)+Math.pow(b,1.5)+Math.pow(a,0.5)
+Math.pow(2.718281828459045,Math.pow(d,f))$
(%o5) done
Is that the expected output?
OP asked about converting %e^x to exp(x). That's easy to do, but to make it stick, we have to disable simplification, i.e. the application of identities which Maxima uses to find a general representation of an expression. By default Maxima simplifies exp(x) to %e^x. We can replace %e^x by exp(x) but we need to disable simplification to prevent it from going back again.
(%i1) simp:false $
(%i2) matchdeclare (xx, all) $
(%i3) defrule (to_exp, %e^xx, Math.exp(xx));
xx
(%o3) to_exp : %e -> Math . exp(xx)
(%i4) apply1 (1 + %e^(x + %e^y), to_exp);
(%o4) 1 + Math . exp(x + Math . exp(y))
Probably you only want to disable simplification (i.e. simp:false) when you are ready to output the expression. But I can imagine situations in which you would have it disabled, e.g. if it is important to output the expression exactly the way it was entered, e.g. x + x instead of 2*x.
I've used a different mechanism to do the replacement here, namely defrule which defines a pattern matching rule. Pattern matching is very useful, and I encourage you to take a look at defrule and matchdeclare in the Maxima documentation.
f(n)= 1 + 2 + 3 + · · + n
g(n) = 3(n^2) + nlogn
Determining f = O(g) or
f = Ω(g) or f = Θ(g)
.As per my effort and understanding one guess It might be f=O(g) as g(n) has a n^2 power which grows faster than n .
Another way : if divided both by n , f(n) will have a constant 1 and g(n) : nlogn which grows faster than constant 1 . so , f=O(g) .
Is that a correct answer?
What actually is scaling property of Big-O ?
How to prove : For any constant c > 0, cf(n) is O(f(n)).
Understanding so far :
cf(n) < (c + k)f(n) holds for all n > 0 and k > 0.
i. Constant factors are ignored.
ii. Only the powers and functions of n should be exploited
It is this ignoring of constant factors that motivates for such a
notation. Which proves f is O(f).
Is this explanation enough to prove that scaling property of Big-O ?
f(n)=O(g(n)) if there is a positive constant c such as.
|f(n)| <= c*|g(n)| for all n>=n(initial)
and since f(n)=(n(n-1))/2 ----> (n^2)
n^2<= n^2 + nlogn (ignore the constants), for all n>=1 then yes your answer is right.
I found this on stack: reversible "binary to number" predicate
But I don't understand
:- use_module(library(clpfd)).
binary_number(Bs0, N) :-
reverse(Bs0, Bs),
binary_number(Bs, 0, 0, N).
binary_number([], _, N, N).
binary_number([B|Bs], I0, N0, N) :-
B in 0..1,
N1 #= N0 + (2^I0)*B,
I1 #= I0 + 1,
binary_number(Bs, I1, N1, N).
Example queries:
?- binary_number([1,0,1], N).
N = 5.
?- binary_number(Bs, 5).
Bs = [1, 0, 1] .
Could somebody explain me the code
Especialy this : binary_number([], _, N, N). (The _ )
Also what does library(clpfd) do ?
And why reverse(Bs0, Bs) ? I took it away it still works fine...
thx in advance
In the original, binary_number([], _, N, N)., the _ means you don't care what the value of the variable is. If you used, binary_number([], X, N, N). (not caring what X is), Prolog would issue a singleton variable warning. Also, what this predicate clause says is that when the first argument is [] (the empty list), then the 3rd and 4th arguments are unified.
As explained in the comments, use_module(library(clpfd)) causes Prolog to use the library for Constraint Logic Programming over Finite Domains. You can also find lots of good info on it via Google search of "prolog clpfd".
Normally, in Prolog, arithmetic expressions of comparison require that the expressions be fully instantiated:
X + Y =:= Z + 2. % Requires X, Y, and Z to be instantiated
Prolog would evaluate and do the comparison and yield true or false. It would throw an error if any of these variables were not instantiated. Likewise, for assignment, the is/2 predicate requires that the right hand side expression be fully evaluable with specific variables all instantiated:
Z is X + Y. % Requires X and Y to be instantiated
Using CLPFD you can have Prolog "explore" solutions for you. And you can further specify what domain you'd like to restrict the variables to. So, you can say X + Y #= Z + 2 and Prolog can enumerate possible solutions in X, Y, and Z.
As an aside, the original implementation could be refactored a little to avoid the exponentiation each time and to eliminate the reverse:
:- use_module(library(clpfd)).
binary_number(Bin, N) :-
binary_number(Bin, 0, N).
binary_number([], N, N).
binary_number([Bit|Bits], Acc, N) :-
Bit in 0..1,
Acc1 #= Acc*2 + Bit,
binary_number(Bits, Acc1, N).
This works well for queries such as:
| ?- binary_number([1,0,1,0], N).
N = 10 ? ;
no
| ?- binary_number(B, 10).
B = [1,0,1,0] ? ;
B = [0,1,0,1,0] ? ;
B = [0,0,1,0,1,0] ? ;
...
But it has termination issues, as pointed out in the comments, for cases such as, Bs = [1|_], N #=< 5, binary_number(Bs, N). A solution was presented by #false which simply modifies the above helps solve those termination issues. I'll reiterate that solution here for convenience:
:- use_module(library(clpfd)).
binary_number(Bits, N) :-
binary_number_min(Bits, 0,N, N).
binary_number_min([], N,N, _M).
binary_number_min([Bit|Bits], N0,N, M) :-
Bit in 0..1,
N1 #= N0*2 + Bit,
M #>= N1,
binary_number_min(Bits, N1,N, M).