I am trying to get the coeficient matrix of the following taylor series expansion
(%i47) SS: taylor( matrix( [sin(h)], [cos(t)] ) , [h,t], [h_0, t_0], 1 );
[ sin(h_0) + cos(h_0) (h - h_0) + . . . ]
(%o47)/T/ [ ]
[ cos(t_0) - sin(t_0) (t - t_0) + . . . ]
Now that I have the taylor expansion I have, I want this to of the form A[h;t] + b.
coefmatrix( SS, [h,t,1] );
[ sin(h_0) + cos(h_0) (h - h_0) + . . . ]
coefmatrix: improper argument: [ ]
[ cos(t_0) - sin(t_0) (t - t_0) + . . . ]
-- an error. To debug this try: debugmode(true);
This last step gives an error. How can I accomplish what I desire?
After some tinkering, the correct way to do this is:
SS(h,t):= taylor( matrix( [sin(h)], [cos(t)] ) , [h,t], [h_0, t_0], 1 );
q: list_matrix_entries( SS( h,t ) );
A: coefmatrix( q, [h, t] );
b: expand( A . [h,t] - q );
this results in:
(%i6) A: coefmatrix( q, [h,t] );
[ cos(h_0) 0 ]
(%o6) [ ]
[ 0 - sin(t_0) ]
(%i7) b: expand( A . [h,t] - q );
[ h_0 cos(h_0) - sin(h_0) ]
(%o7) [ ]
[ (- t_0 sin(t_0)) - cos(t_0) ]
All in all the function list_matrix_entries was the key.
Related
The following is my script:
binom_df(s, N, p):= ( N! / ( s! * (N-s)! ) ) * ( p^s ) * ( 1 - p) ^ ( N-s );
ddn_binom(s, N, p):= diff( binom_df(s, N, p), s);
ddn_binom(s, N, p);
wxplot2d(
[ binom_df( s, 100, 1/2 ), ddn_binom( s, 100, 1/2 ) ],
[s, 30, 70],
[ box, false ],
[ legend, "N=100, p= 1/2",
"diff( binom_df( s, N, p ) )"
],
[ xlabel, "s"]
);
for s: 1 thru 10 step 1 do (
display(s),
pr_val: ev( ( ddn_binom( s1, N1, p1 ) ), s1=s, N1=100, p1=0.5 ),
display(pr_val)
);
ddn_binom( 10, 100, 1/10 );
The call to ddn_binom works in wxplot2d; I get the graph. But the call to ddn_binom in the 'for' loop fails with the message:
diff: second argument must be a variable; found 1
which is the reason I have the last line with constant arguments
but it too fails with the message:
diff: second argument must be a variable; found 10
I am not conversant with the basic principles of how Maxima works.
If you can help me, I would appreciate it.
Thanks.
BD
(%i1) binom_df(s, N, p):= ( N! / ( s! * (N-s)! ) ) * ( p^s ) * ( 1 - p) ^ ( N-s ) $
(%i2) ddn_binom1(s, N, p):= diff(binom_df(s, N, p), s) $
In (%i2) the function body is not evaluated. It is
evaluated every time the function ddn_binom1 is called with the
formal argument s assigned to an actual argument. diff fails if s is a number.
You can use define if you want the body evaluated
(%i3) define(ddn_binom2(s, N, p), diff(binom_df(s, N, p), s)) $
fundef returns the function definitions:
(%i4) display2d: false $
(%i5) fundef(ddn_binom1);
(%o5) ddn_binom1(s,N,p):=diff(binom_df(s,N,p),s)
(%i6) fundef(ddn_binom2);
(%o6) ddn_binom2(s,N,p):=(-(N!*(1-p)^(N-s)*p^s*psi[0](s+1))/((N-s)!*s!))
+(N!*(1-p)^(N-s)*p^s*psi[0]((-s)+N+1))/((N-s)!*s!)
-(N!*log(1-p)*(1-p)^(N-s)*p^s)/((N-s)!*s!)
+(N!*(1-p)^(N-s)*p^s*log(p))/((N-s)!*s!)
Links to the documentation: operator :=, define, fundef
Don't use the variable s in your loop, because it's used in function.
You can first get a function and then evaulate:
for t: 1 thru 10 step 1 do (
display(t),
pr_val: ddn_binom( s1, 100, 0.5 ),
pr_val:ev(pr_val,s1=t),
display(pr_val)
);
You can also use subst:
for t: 1 thru 10 step 1 do (
display(t),
pr_val: subst(t,s1,ddn_binom( s1, 100, 0.5 )),
display(pr_val)
);
I am trying to manipulate some algebra with Maxima, here is my code :
Pd: matrix( [a00, a01, a02, a03], [a10, a11,a12,a13], [a20,a21,a22,a23]);
Pdd: matrix( [b00, b01, b02, b03], [b10, b11,b12,b13], [b20,b21,b22,b23]);
T1: col( Pd, 1) . transpose( col(Pdd,4) ) - col( Pd, 4 ) . transpose( col(Pdd,1) );
T2: col( Pd, 2) . transpose( col(Pdd,4) ) - col( Pd, 4 ) . transpose( col(Pdd,2) );
T3: col( Pd, 3) . transpose( col(Pdd,4) ) - col( Pd, 4 ) . transpose( col(Pdd,3) );
t: matrix( list_matrix_entries( T1 ), list_matrix_entries( T2 ), list_matrix_entries( T3 ) );
transpose( list_matrix_entries( t ) );
This finally gives 27 equations, something like :
matrix([a00*b03−a03*b00],
[a00*b13−a03*b10],
[a00*b23−a03*b20],
[a10*b03−a13*b00],
[a10*b13−a13*b10],
[a10*b23−a13*b20],
[a20*b03−a23*b00],
[a20*b13−a23*b10],
[a20*b23−a23*b20],
[a01*b03−a03*b01],
[a01*b13−a03*b11],
[a01*b23−a03*b21],
[a11*b03−a13*b01],
[a11*b13−a13*b11],
[a11*b23−a13*b21],
[a21*b03−a23*b01],
[a21*b13−a23*b11],
[a21*b23−a23*b21],
[a02*b03−a03*b02],
[a02*b13−a03*b12],
[a02*b23−a03*b22],
[a12*b03−a13*b02],
[a12*b13−a13*b12],
[a12*b23−a13*b22],
[a22*b03−a23*b02],
[a22*b13−a23*b12],
[a22*b23−a23*b22])
My question is, how is it possible to manipulate (rearrange) these 27 equations with Maxima into the form t=Ea, where a is all the a.. entries stacked as column vector.
Try this:
coefmatrix (list_matrix_entries (foo),
[a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23]);
where foo is the result of transpose( list_matrix_entries( t ) ); above.
In Maxima I have an equation that is like:
eq : c0*a + d0*a + c1*b - c2*p - c4*q = c5*r
Is there a command that allows me to arrive at:
(c0 + d0)*a + c1*b = c2*p + c4*q + c5*r
In short, I want to choose which variables end on either the left or right hand side and I want to write it such that there is only one occurence of the variables I select (in this case a, b, p, q, r).
Perhaps coefmatrix is useful for this.
(%i1) display2d : false $
(%i2) eq : c0*a + d0*a + c1*b - c2*p - c4*q = c5*r $
(%i3) vars : [a, b, p, q, r] $
(%i4) coeffs : coefmatrix ([eq], vars);
(%o4) matrix([d0+c0,c1,-c2,-c4,-c5])
(%i5) coeffs . vars;
(%o5) (-c5*r)-c4*q-c2*p+a*(c0+d0)+b*c1
Note that both arguments of coefmatrix must be lists.
I was on the site rise4fun a few weeks ago and they had a python code that converted a sudoku puzzle input file to z3. I checked again today and the file is gone, and was wondering if anyone had this code or could explain to me how to implement it. Thanks!!
# 9x9 matrix of integer variables
X = [ [ Int("x_%s_%s" % (i+1, j+1)) for j in range(9) ]
for i in range(9) ]
# each cell contains a value in {1, ..., 9}
cells_c = [ And(1 <= X[i][j], X[i][j] <= 9)
for i in range(9) for j in range(9) ]
# each row contains a digit at most once
rows_c = [ Distinct(X[i]) for i in range(9) ]
# each column contains a digit at most once
cols_c = [ Distinct([ X[i][j] for i in range(9) ])
for j in range(9) ]
# each 3x3 square contains a digit at most once
sq_c = [ Distinct([ X[3*i0 + i][3*j0 + j]
for i in range(3) for j in range(3) ])
for i0 in range(3) for j0 in range(3) ]
sudoku_c = cells_c + rows_c + cols_c + sq_c
# sudoku instance, we use '0' for empty cells
instance = ((5,3,0,0,7,0,0,0,0),
(6,0,0,1,9,5,0,0,0),
(0,9,8,0,0,0,0,6,0),
(8,0,0,0,6,0,0,0,3),
(4,0,0,8,0,3,0,0,1),
(7,0,0,0,2,0,0,0,6),
(0,6,0,0,0,0,2,8,0),
(0,0,0,4,1,9,0,0,5),
(0,0,0,0,8,0,0,7,9))
instance_c = [ If(instance[i][j] == 0,
True,
X[i][j] == instance[i][j])
for i in range(9) for j in range(9) ]
s = Solver()
s.add(sudoku_c + instance_c)
if s.check() == sat:
m = s.model()
r = [ [ m.evaluate(X[i][j]) for j in range(9) ]
for i in range(9) ]
print_matrix(r)
else:
print "failed to solve"
I am trying to multiply 2 matrices of 2*2 order. One of the matrix contains an unknown parameter "k1". I want to check a satisfiable solution that for which value of k1. The product of two matrices will be equal to the third one.
Note: I dont want to convert the multiplication into a linear relation or set of equation I want to manipulate it as matrices.
Here is where I am stuck.
k1 = Int ('k1')
x = [ [ Int("x_%s_%s" % (i+1, j+1)) for j in range(2) ]
for i in range(2) ]
a =((1,k1),(3,4))
b =((1,1),(1,1))
c= ((3,3),(7,7))
s = Solver()
s.add(a[0][1]>0)
s.add(a*b==c)
if s.check() == sat:
m = s.model()
r = [ [ m.evaluate(x[i][j]) for j in range(2) ]
for i in range(2) ]
print_matrix(r)
else:
print "failed to solve"
Any way Out?
One possible solution is
k1 = Int ('k1')
x = [ [ Int("x_%s_%s" % (i+1, j+1)) for j in range(2) ]
for i in range(2) ]
a =((1,k1),(3,4))
b =((1,1),(1,1))
c= ((3,3),(7,7))
s = Solver()
eq1= a[0][1]>0
eq2 =[[sum(a[i][k]*b[k][j] for k in range(2)) == c[i][j] for i in range(2) ]
for j in range(2) ]
s.add(eq1)
s.add(eq2[0][0])
s.add(eq2[0][1])
s.add(eq2[1][0])
s.add(eq2[1][1])
print s
print s.check()
m = s.model()
print m
and the corresponding output is
[k1 > 0, 1 + k1*1 == 3, True, 1 + k1*1 == 3, True]
sat
[k1 = 2]
Please run this example online here