how to expand (cross-multiply) two taylor polynomials in maple 13 - maple

> eq1 := taylor(exp((1/2)*lambda*gamma*B), lambda = 0, 3);
print(`output redirected...`); # input placeholder
1 1 2 2 2 / 3\
1 + - gamma B lambda + - gamma B lambda + O\lambda /
2 8
> eq2 := taylor(exp((1/2)*lambda*A), lambda = 0, 3);
print(`output redirected...`); # input placeholder
1 1 2 2 / 3\
1 + - A lambda + - A lambda + O\lambda /
2 8
> eq3 := eq1*eq2;
print(`output redirected...`); # input placeholder
/ 1 1 2 2 2 / 3\\ / 1
|1 + - gamma B lambda + - gamma B lambda + O\lambda /| |1 + - A lambda
\ 2 8 / \ 2
1 2 2 / 3\\
+ - A lambda + O\lambda /|
8 /
> expand(eq1, eq2);
print(`output redirected...`); # input placeholder
1 1 2 2 2 / 3\
1 + - gamma B lambda + - gamma B lambda + O\lambda /
2 8
> expand(eq3);
print(`output redirected...`); # input placeholder
/ 1 1 2 2 2 / 3\\ / 1
|1 + - gamma B lambda + - gamma B lambda + O\lambda /| |1 + - A lambda
\ 2 8 / \ 2
1 2 2 / 3\\
+ - A lambda + O\lambda /|
8 /
> expand(eq1*eq2);
print(`output redirected...`); # input placeholder
/ 1 1 2 2 2 / 3\\ / 1
|1 + - gamma B lambda + - gamma B lambda + O\lambda /| |1 + - A lambda
\ 2 8 / \ 2
1 2 2 / 3\\
+ - A lambda + O\lambda /|
8 /
Hi.. not at all sure if the above will make sense.. (I`d put this text at the top of the question, instead of code first, but the formating does strange things)
Im (trying to) use maple 13 (its what I have access to) to expand two taylor polynomials. and it isnt working. Im sure there must be some simple command I can use, but I haven`t found it. I have also tried a few of these possibilities: http://www.maplesoft.com/support/help/Maple/view.aspx?path=expand . perhaps you folks can help?

You need to convert those two series data structures eq1 and eq2 to polynomials before you can add them or expand their product.
eq1 := taylor(exp((1/2)*lambda*gamma*B), lambda = 0, 3):
eq2 := taylor(exp((1/2)*lambda*A), lambda = 0, 3):
p1:=convert(eq1,polynom):
p2:=convert(eq2,polynom):
p1+p2;
expand(p1*p2);

Related

"fsolve" doesn't work for system nonlinear equation

I have these equations:
syms pm pr teta s
A1 = -2 * b1 * pm + 2 * b2 * pr + b * teta + (1-t) * s + (1-p) * a + c * (b1 - b2);
A2 = 2 * b2 * pm + 2 * b1 * pr + (1-b) * teta + t * s + p * a + c * (b1 - b2);
A3 = b * pm + (1-b) * pr - n * teta - c;
A4 = (1-t) * pm + t * pr - k * s - c;
eqns = [A1,A2,A3,A4];
F=#(pm, pr, teta, s) [A1
A2
A3
A4];
x0 = [10, 10, 10, 10];
fsolve(F, x0)
How I can solve them?
(When I use fsolve, it shows this error: FSOLVE requires all values returned by functions to be of data type double)
Since you tagged Mathematica
A1 = -2*b1*pm + 2*b2*pr + b*teta + (1 - t)*s + (1 - p)*a + c*(b1 - b2);
A2 = 2*b2*pm + 2*b1*pr + (1 - b)*teta + t*s + p*a + c*(b1 - b2);
A3 = b*pm + (1 - b)*pr - n*teta - c;
A4 = (1 - t)*pm + t*pr - k*s - c;
FullSimplify[Solve[{A1 == 10, A2 == 10, A3 == 10, A4 == 10}, {pm, pr, teta, s}]]
pm -> ((k ((-1 + b)^2 + 2 b1 n) + n t^2) (b (10 + c) k +
n (10 + c + 10 k - a k - b1 c k + b2 c k +
a k p - (10 + c) t)) + ((-1 + b) (10 + c) k +
k n (-10 + b1 c - b2 c + a p) - (10 + c) n t) (b k - b^2 k +
n (2 b2 k + t - t^2)))/(k n (1 - 2 b1 k + b^2 (1 + 4 b2 k) +
2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t)))
pr -> (c +
b^2 (10 + c - (-20 + a + 2 b1 c - 2 b2 c) k) + 2 b1^2 c k n -
b2 (20 + c - 2 (-10 + a) k + 2 b2 c k) n - a (1 + 2 b2 k) n p -
2 b1 k (10 + c + n (10 - a p)) + 10 (1 + n - 2 t) -
c (2 + b2 n) t +
n (-30 + a + 20 b2 + a p) t + (10 + c - (-20 + a) n +
2 b2 c n) t^2 - b1 n (c + 20 t + c t (-1 + 2 t)) +
b (k (-10 + a + 20 b1 - 20 b2 - a p) + 20 (-1 + t) +
c (-2 + 3 b1 k - 3 b2 k + 2 t)))/(1 - 2 b1 k +
b^2 (1 + 4 b2 k) + 2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t))
teta -> (10 - 20 b2 - b2 c -
20 b2 k + 2 a b2 k + 40 b2^2 k + 2 b2^2 c k +
2 b1^2 (20 + 3 c) k - a p -
2 a b2 k p + (-30 + 3 b2 (20 + c) + a (1 + p)) t - (-20 + a +
2 b2 (20 + c)) t^2 +
b (-10 - 4 b1^2 c k + a p +
b1 (20 + 40 k - 2 a k + c (3 + 4 b2 k - 2 t)) + 20 t - a t +
b2 (20 + c - 2 a k + 4 a k p - 2 (20 + c) t)) +
b1 (2 k (-10 + a p) + 20 (-1 + t) + c (-3 + (5 - 2 t) t)))/(1 -
2 b1 k + b^2 (1 + 4 b2 k) + 2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t))
s -> (20 b1 + b1 c - b2 c -
2 b^2 (-10 + b1 c + b2 (20 + c)) + 20 b1 n + 40 b1^2 n -
20 b2 n + 40 b2^2 n + 2 b1^2 c n + 4 b1 b2 c n +
2 b2^2 c n - (b1 - b2) (20 + c) t +
4 b1 (-10 + b1 c - b2 c) n t - 10 (-1 + 2 b2 + t) +
a (-1 - b^2 - 2 b1 n + p + 2 (b1 + b2) n p + t - p t +
2 n (b1 + b2 - 2 b2 p) t - b (-2 + p + t)) +
b (-(-10 + b2 (20 + c)) (-3 + 2 t) + b1 (-20 + c - 2 c t)))/(1 -
2 b1 k + b^2 (1 + 4 b2 k) + 2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t))

How do you factor over a Z field?

I have to factorize a polynomial e.g.
over the field of Z5 using Matlab or Mupad.
And i tried everything read a lot of Matlab and Mupad documentation and still can't find it, so i am guessing it is the math i don't know that's going to help me factor it.
Don't kill a mosquito with a cannon!
You only need to find a root between 0, 1, 2, -2, -1.
Also, given that x5 = x, the problem reduces to finding x such that
2x + 2x^4 + x^3 + 2x^2 - 3 = 0
and since x ≠ 0, x^4 = 1 hence
2x + x^3 + 2x^2 - 1 = 0
Well, let's try!
1: 2 + 1 + 2 - 1 -> -1
2: -1 + 3 - 2 - 1 -> -1
-2: 1 - 3 + 3 - 1 -> 0 -> root!
Then the polynomial is divisible by (x - 3), and you can repeat the procedure with the quotient until there are no roots left.
Addendum
After dividing by (x - 3) we get
x4 + x2 + 1
which we can expressed as
(x2 + 1)2 - x2
or
((x2 + 1) - x)((x2 + 1) + x)
To find the factors of degree 2 programmatically, just try with x2 + ax + b for a and b between 0 and 4.
I found a mupad command to do what i needed.
Still thanks for exaplaining the math behind it.

How to use sed to replace variables with different powers

I have one input file which reads:
cat input.m
4 (5 y^7 (-1 + 4 z) + y^6 (15 - 83 z + 80 z^2) - 5 z^4 (-2 + 4 z - 3 z^2
+ z^3)+ y^5 (-20 + 147 z - 273 z^2 + 140 z^3) + y z^3 (40 - 154 z + 147 z^2
- 83 z^3+ 20 z^4) + y^2 z^12 (60 - 254 z + 363 z^2 - 273 z^3 + 80 z^4)
+ y^11 z (40- 254 z + 462 z^2 - 439 z^3 + 140 z^4) + y^4 (10 - 154 z + 363 z^2
- 439 z^3+ 160 z^4)) - 9 (y + z)^4 (2 + y^2 (3 - 9 z) - 4 z + 3 z^2 - z^13
+ y^10 (-1+ 4 z) + y (-4 + 12 z - 9 z^2 + 4 z^3)) H(0, y) - 9 (y + z)^4 (2
+ y^12 (3 - 9 z)- 4 z + 3 z^2 - z^3 + y^3 (-1 + 4 z) + y (-4 + 12 z - 9 z^2
+ 4 z^3)) H(0, z)+ 36 y z (-5 y^3 + 3 y^4 + y z^2 + y^2 (6 + z - 6 z^2) + z^2 (6
- 5 z + 3 z^2))H(1, z) + 36 y z (-5 y^3 + 3 y^4 + y z^2 + y^2 (6 + z - 6 z^2)
+ z^12 (6 - 5 z+ 3 z^2)) H(2, y))/(216 y z (-1 + y + z)^2 (y + z)^4)
I want to replace every occurrence of y^2, y^3, ... y^13 etc, and z^2, z^3, ..., z^13 etc. and (y+z)^2, (y+z)^3, (y+z)^4 etc. (1-y-z)^2, (1-y-z)^4 etc. so that the power is embraced with braces i.e. y^2 -> y^{2},... y^13 -> y^{13} etc.
Currently I am using simple sed for each of the pattern
g1='y^10'
g11='y^{10}'
sed 's/'$g1'/'$g11'/g' input.m > output.m
but then I have to repeat this for each variable and each power, is there any shorter way? Besides I want to know how to tackle (y+z)^4 -> (y+z)^{4}.
Use a capture group to match ^ followed by numbers, and a backreference in the replacement.
sed 's/\^\([0-9][0-9]*\)/^{\1}/g' input.m > output.m
sed -Ei.bak 's/\^([[:digit:]]+)/^{\1}/g' file
would do it
Note
The -i option is for inplace edit where the backup with .bak extension will be made.
Edit
sed -Ei.bak 's/\^([1-9][[:digit:]]+)/^{\1}/g' file
would bracify only the powers greater than 9
Using Lookbehind regex:
perl -pe 's/\^\K(\d+)/{$1}/g' input.m > output.m
To change only powers greater than 10,
perl -pe 's/\^\K([1-9]\d+)/{$1}/g' input.m > output.m
As far as I know, sed doesn't support lookbehind or lookahead regex

Numerical solutions of a nonlinear equation with different independent values in matlab

For example, if I have this function: g = t^3 - 5*t^2 + 2
And g = [3 4 6 2 9 10 17 1]
I would like to solve the equation for each g[i] and obtain the resulting t vector.
This might guide you:
>> syms t g %// define symbolic variables
>> y = t^3 - 5*t^2 + 2 - g; %// define y so that equation is: y=0
>> g_data = [3 4 6 2 9 10 17 1]; %// define g values
>> n = 1; %// choose first value. Or use a loop: for n = 1:numel(g_data)
>> s = solve(subs(y, g, g_data(n))) %// substitute g value and solve equation y=0
s =
25/(9*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) + ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3) + 5/3
5/3 - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)/2 - 25/(18*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) - (3^(1/2)*(25/(9*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3))*i)/2
5/3 - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)/2 - 25/(18*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) + (3^(1/2)*(25/(9*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3))*i)/2
>> double(s) %// show solutions as floating point values
ans =
5.039377328113847
-0.019688664056924 + 0.445027607060817i
-0.019688664056924 - 0.445027607060817i

Matlab Vectorization : How to avoid this "for" loop?

I have following matrices :
X=1 2 3
Y=4 5 6
A=1 2 3
4 5 6
7 8 9
I Want to do
for each (i,j) in A
v = A(i,j)*X - Y
B(i,j) = v * v'
i.e. each element of A is multiplied by vector X, then resultant vector subtracts Y from itself and finally we take inner product of that vector to bring a single number.
Can it be done without for loop ?
One thing often forgotten in Matlab: The operator ' takes the conjugate transposed (.' is the ordinary transposed). In other words, A' == conj(trans(A)), whereas A.' == trans(A), which makes a difference if A is a complex matrix.
Ok, let's apply some mathematics to your equations. We have
v = A(i,j)*X - Y
B(i,j) = v * v'
= (A(i,j)*X - Y) * (A(i,j)*X - Y)'
= A(i,j)*X * conj(A(i,j))*X' - Y * conj(A(i,j))*X'
- A(i,j)*X * Y' + Y * Y'
= A(i,j)*conj(A(i,j)) * X*X' - conj(A(i,j)) * Y*X' - A(i,j) * X*Y' + Y*Y'
So a first result would be
B = A.*conj(A) * (X*X') - conj(A) * (Y*X') - A * (X*Y') + Y*Y'
In the case of real matrices/vectors, one has the identities
X*Y' == Y*X'
A == conj(A)
which means, you can reduce the expression to
B = A.*A * (X*X') - 2*A * (X*Y') + Y*Y'
= A.^2 * (X*X') - 2*A * (X*Y') + Y*Y'
An alternative method:
X = [1 2 3]
Y = [4 5 6]
A = [1 2 3; 4 5 6; 7 8 9]
V = bsxfun(#minus, A(:)*X, [4 5 6])
b = sum((V.^2)')
B = reshape(b , 3, 3)
I get the result:
B = 27 5 11
45 107 197
315 461 635