How to define some variables as non-commutative in Maxima - symbolic-math

For example, I'd like to define x and y as non-commutative, and a and b as commutative (as usual). In other words,
x y ≠ y x, a x = x a, a b = b a .
Further,
(x + a y) (x - a y) = x^2 + a (y x - x y) - a^2 y^2 .
What is a code for defining x and y, and a symbol for multiplication (such as * and . ) ?

You can work with Maxima's commutative * and non-commutative . products in the way that you want by following the next two steps:
Declare the symbols a and b as scalars:
declare([a, b], scalar)$
Enable dotscrules:
dotscrules: true$
This simplifies non-commutative products involving scalars to commutative products (i.e., a.x becomes a*x).
Now you are ready. For example,
expand((a*x + b*y) . (a*x - b*y))
returns
a*b*y.x - b^2*y^^2 - a*b*x.y + a^2*x^^2
(note that ^^ is the non-commutative exponentiation operator).

Related

Variable elimination in Bayes Net on a node with single child

If we have a node X, that has a child Y in a Bayes net, why is it correct to express P(Y) as P(Y|X)P(X)? Does it then follow that X is a necessary condition for Y?
Bayes Networks
An edge in a Bayes Network means the variable is conditionally dependent. If nodes are not connected by any path, they are conditionally independent.
Having a node X with a child Y means you need to learn:
Given X is True, what is the probability of Y being True?
Given X is False, what is the probability of Y being True?
More generally: If X can have n values and Y can have m values, then you have to learn n * (m - 1) values. The - 1 is there because the probabilities need to sum up to 1.
Example
Let's stick with the simple case that both variables are binary and use the following from Wikipedia:
Say X is RAIN and Y is SPRINKLER. You want to express Y (SPRINKLER) in terms of X (RAIN).
The Bayes theorem states:
P(Y|X) = P(X|Y) * P(Y) / P(X)
<=> P(Y) = P(Y | X) * P(X) / P(X | Y)
Now we apply the Law of total probability for X. This means, for X we simply go through all possible values:
P(Y) = P(Y | X = true) * P(X = true) +
P(Y | X = false) * P(X = false)
I guess this is what you refer to. P(X=true | Y) = 1, because the X=true means we already know that X=true happened. It doesn't matter what Y is.
To continue our case, we now look up the values in the tables (X is RAIN, Y is SPRINKLER):
P(Y) = 0.01 * 0.2 + 0.4 * 0.8
= 0.322

Understanding the math behind the code

I have a really basic and simple question but I am having problems with understanding this C code.
#define POLYNOMIAL(x) \
(((((3.0 * (x) + 2.0) * (x) - 5.0) * (x) - 1.0) * (x) + 7.0) * (x) - 6.0)
This definition is for this polynomial: 3x5+2x4-5x3-x2+7x-6
How can I convert this polynomial into the form shown in the #define? Is there any trick for this?
Your polynomial:
3x5 + 2x4 - 5x3 - x2 + 7x - 6
Can be rewritten successively:
(3x4 + 2x3 - 5x2 - x + 7) · x - 6
((3x3 + 2x2 - 5x - 1) · x + 7) · x - 6
(((3x2 + 2x - 5) · x - 1) · x + 7) · x - 6
((((3x + 2) · x - 5) · x - 1) · x + 7) · x - 6
This an expanded, or unrolled, Horner's Method loop. If the coefficients were expressed as an array:
double polynomial[] = { -6, 7, -1, -5, 2, 3 };
Then, the polynomial could be evaluated with this function:
double horners (double poly[], int terms, double x) {
double result = 0;
while (terms--) {
result = result * x + poly[terms];
}
return result;
}
Just add parenthesis and decrease out powers inside until you get to the last one, like this:
(3x^5)+(2x^4)-(5x^3)-(x^2)+7x-6
((3x^4)+(2x^3)-(5x^2)-x+7)x-6
(((3x^3)+(2x^2)-5x-1)x+7)x-6
((((3x^2)+2x-5)x-1)x+7)x-6
((((3x+2)x-5)x-1)x+7)x-6

How to find whether two line segment(not two straight lines) intersect

I want to find a way to check whether two line segments intersects or not.I am using Xlib programming to implement this.
I checked on internet but i only found the ways to find the intersection point of two lines but not of two line segments.
How can i implement this using X lib programming?
You don't need Xlib for this. Let the two segments be
A1 = (x1, y1) -> B1 = (x1 + dx1, y1 + dy1) and
A2 = (x2, y2) -> B2 = (x2 + dx2, y2 + dy2).
Let
vp = dx1 * dy2 - dx2 * dy1
If vp == 0 the segments are parallel and there is no intersection.
Otherwise, let v = (vx, vy) be the vector between A1 and A2
vx = x2 - x1
vy = y2 - y1
Compute
k1 = (vx * dy2 - vy * dx2) / vp
k2 = (vx * dy1 - vy * dx1) / vp
If either k1 or k2 fall outside the [0, 1] interval, the segments don't intersect (but the underlying lines do intersect). Otherwise, the intersection is at
(x1 + k1 * dx1, y1 + k1 * dy1)
which incidentally, if you wonder about symmetry, will be the same point as
(x2 + k2 * dx2, y2 + k2 * dy2)
These formulas are basically similar to the answer on How do you detect where two line segments intersect? except coding from there would not necessarily be trivial for either a newbie or someone in a rush (like I have been myself many times).

Differentiating sums with Maxima

I have the following sum:
sum((R[i]-(a*X[i]+b)*t + 1/2*(c*X[i]+d)^2*t)^2/((c*X[i]+d)^2*t), i, 1, N);
which I want to differenciate wrt. a:
diff(%, a);
but Maxima (wxMaxima to be precise) just prints d/da . Can I
make it actually differentiate the sum (so because N is finite is
should differentiate every element in the sum separately)?
If I set N to some constant, e.g.:
sum((R[i]-(a*X[i]+b)*t + 1/2*(c*X[i]+d)^2*t)^2/((c*X[i]+d)^2*t), i, 1, 100);
then I get explicit sum of 100 elements (takes about 2 pages), and
then differentiation works (but again I get 2 pages instead of a small
sum). Can I get this result displayed as a sum?
Which version of Maxima do you use ?
Here is my session of Maxima with you equation differentiated wrt.a and than substituted to N=100.
~$ maxima
Maxima 5.24.0 http://maxima.sourceforge.net
using Lisp SBCL 1.0.51
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) sum((R[i]-(a*X[i]+b)*t + 1/2*(c*X[i]+d)^2*t)^2/((c*X[i]+d)^2*t), i, 1, N);
2
(c X + d) t
N i 2
==== (------------- - (a X + b) t + R )
\ 2 i i
> ------------------------------------
/ 2
==== (c X + d)
i = 1 i
(%o1) ------------------------------------------
t
(%i2) diff(%, a);
2
(c X + d) t
N i
==== X (------------- - (a X + b) t + R )
\ i 2 i i
(%o2) - 2 > --------------------------------------
/ 2
==== (c X + d)
i = 1 i
(%i3) %, N=100;
2
(c X + d) t
100 i
==== X (------------- - (a X + b) t + R )
\ i 2 i i
(%o3) - 2 > --------------------------------------
/ 2
==== (c X + d)
i = 1 i

tsql conditional maths equation

I need to perform some summations during a select query however depending on 2 values i will need to perform a different equation. hopefully an example will demonstrate
basically i need to perform the following summations
if x > y then (x - y + z) or
if x < y then (x - x + z) basically i am setting this to 0.
So far i thought that i could use 2 tables to dump the x > y values and the x < y values and then perform the relevant equations.
any ideas
You can use a case expression.
select case
when x > y then x - y + z
when x < y then x - x + z
else 0 -- x = y
end
from YourTable

Resources