When I use Maxima to calculate the Taylor series:
f(x,y) := taylor((x+y)^3, [x, y], [2, 3], 2);
f(2,3); /* error: wrong number of arguments */
Basically I want to define a function as a expansion of (x+y)^3, which takes in x,y as parameter. How can I achieve this?
Try
(%i1) f(x,y) := ''(ratdisrep(taylor(('x+'y)^3, ['x, 'y], [2, 3], 2))) $
(%i2) f(2, 3);
(%o2) 125
or
(%i1) define(f(x, y), ratdisrep(taylor(('x+'y)^3, ['x, 'y], [2, 3], 2)))$
(%i2) f(2, 3);
(%o2) 125
Related
Minimal example is the following: Given a set of possible integers [1, 2, 3] create an arbitrary list of size 5 using z3py. Duplicates are allowed.
The expected result is something like [1, 1, 1, 1, 1] or [3, 1, 2, 2, 3], etc.
How to tackle this problem and how to implement 'choosing'? Finally, I would like to find all solutions which can be done by adding additional constraints as explained in link. Any help will be very appreciated.
The following should work:
from z3 import *
def choose(elts, acceptable):
s = Solver()
s.add(And([Or([x == v for v in acceptable]) for x in Ints(elts)]))
models = []
while s.check() == sat:
m = s.model ()
if not m:
break
models.append(m)
block = Not(And([v() == m[v] for v in m]))
s.add(block)
return models
print choose('a b c d e', [1, 2, 3])
The second part of the output of the eigenvectors function in Maxima is a list of the eigenvectors which correspond to the eigenvalues of the first part.
E.g.:
[[[1,-1/4],[1,1]],[[[1,2/3]],[[1,-1]]]]
(1,2/3) is the eigenvector of eigenvalue 1, and (1,-1) is the eigenvector of eigenvalue (-1/4).
How can I turn these vectors into a matrix (in this case it would be equivalent to matrix([1,1],[2/3,-1])).
Thanks
(%i1) display2d: false $
(%i2) r: [[[1,-1/4],[1,1]],[[[1,2/3]],[[1,-1]]]] $
(%i3) s: second(r) $
(%i4) s: map('first, s) $
(%i5) s: apply('maplist, cons("[", s)) $
(%i6) s: apply('matrix, s);
(%o6) matrix([1,1],[2/3,-1])
Here's an attempt. Notice I've extracted the pieces via multiple assignment first, so that it's easy to remember what the pieces mean.
(%i1) foo : [[[1,-1/4],[1,1]],[[[1,2/3]],[[1,-1]]]] $
(%i2) [[vals, mults], vecs] : foo;
1 2
(%o2) [[[1, - -], [1, 1]], [[[1, -]], [[1, - 1]]]]
4 3
(%i3) vals;
1
(%o3) [1, - -]
4
(%i4) mults;
(%o4) [1, 1]
(%i5) vecs;
2
(%o5) [[[1, -]], [[1, - 1]]]
3
(%i6) apply (append, vecs);
2
(%o6) [[1, -], [1, - 1]]
3
(%i7) apply (matrix, apply (append, vecs));
[ 2 ]
[ 1 - ]
(%o7) [ 3 ]
[ ]
[ 1 - 1 ]
(%i8) transpose (%);
[ 1 1 ]
[ ]
(%o8) [ 2 ]
[ - - 1 ]
[ 3 ]
Not sure if that will work when the number of eigenvectors is different from number of eigenvalues and other special cases. But I hope this gives you something to go on.
Can this matrix be generated in a less manual way? It's okay for 4 x 4, but I need something larger. Thanks
--> L : matrix([L11,L12,L13,L14],[L21,L22,L23,L24],[L31,L32,L33,L34],[L41,L42,L43,L44]);
(L) matrix(
[L11, L12, L13, L14],
[L21, L22, L23, L24],
[L31, L32, L33, L34],
[L41, L42, L43, L44]
)
Answer to the question and note the noun form for L in the concat function ('L)
L:genmatrix(lambda([i,j], concat('L,i,j)), 3, 3);
(L) matrix(
[L11, L12, L13],
[L21, L22, L23],
[L31, L32, L33]
)
For a diagonal matrix
R:genmatrix(lambda([i,j], if i=j then concat('R,i) else 0), 3, 3);
(R) matrix(
[R1, 0, 0],
[0, R2, 0],
[0, 0, R3]
)
As coming from python I'm looking for something equivalent to this python code (sets) in delphi5:
>>> x = set("Hello")
>>> x
set(['H', 'e', 'l', 'o'])
>>> y = set("Hallo")
>>> y
set(['a', 'H', 'l', 'o'])
>>> x.intersection(y)
set(['H', 'l', 'o'])
var
a, b, c: set of byte;
begin
a := [1, 2, 3, 4];
b := [3, 4, 5, 6];
c := a*b; // c is the intersection of a and b, i.e., c = [3, 4]
But beware:
var
a, b, c: set of integer;
will not even compile; instead, you get the 'Sets may have at most 256 elements' error. Please see the documentation for more information on Delphi sets.
Update
Sorry, forgot to mention the 'obvious' (from the point of view of a Delphi programmer):
var
a, b, c: set of char;
begin
a := ['A', 'B', 'C', 'D'];
b := ['C', 'D', 'E', 'F'];
c := a*b; // c is the intersection of a and b, i.e., c = ['C', 'D']
But your chars will all be byte chars -- that is, forget about Unicode (Delphi 5 doesn't support Unicode, so in this case this isn't really a restriction)!
I have two arrays:
a = [6, 4, 3]
b = [1, 3, 4]
I call a.sort:
a.sort = [3, 4, 6]
How do I sort array b so the values have the same position to values in array a before the sort?
It would be now:
b = [4, 3, 1]
So that values in b have the same position to values in array a.
You could combine both the arrays into one using the zip method. Once you combine a and b, you would get,
[[6, 1], [4, 3], [3, 4]]
Now sort the arrays, which would sort them based on the first element of each sub-array resulting in,
[[3, 4], [4, 3], [6, 1]]
Now we want to do the reverse of zip to get the first and second elements of each sub-array into a new array. Using transpose, we can get it back in the original form as,
[[3, 4, 6], [4, 3, 1]]
Thankfully using parallel assignment all of this is possible in one line. Here's the full code,
x, y = a.zip(b).sort.transpose
Now x should contain [3, 4, 6], and y should contain [4, 3, 1].
a = [6, 4, 3]
b = [1, 3, 4]
ra, rb = a.zip(b).sort_by(&:first).transpose
# ra => [3, 4, 6]
# rb => [4, 3, 1]
I don't know what you're trying to achieve, and I'm sure others could come up with a more elegant solution, but I would use a Hash instead. Assign the values of a a as the key, and the values of b as the values. You could iterate over a to accomplish this, or just in advance when you're creating this data. The result should be:
$ hash
=> {6 => 1, 4 => 3, 3 => 4}
a0 b0 a1 b1 a2 b2
$ hash.sort
=> [[3, 4], [4, 3], [6, 1]]
Like I said, not super smooth, but I've got turkey hangover...
[a, b].transpose.sort { |x, y| x[0] <=> y[0] }.transpose[1]
=> [4, 3, 1]
or
a, b = [a, b].transpose.sort { |x, y| x[0] <=> y[0] }.transpose