Suppose there are two lists a = {a1, a2, a3} and b = {b1, b2, b3}, and I want to write an assignment statement to make a1=b1,a2=b2,a3=b3 which only refers to a and b:
Thread[a = b]
But it only makes a={b1,b2,b3}. Using := (SetDelayed) instead of = doesn't work either.
Any solution? Thanks.
I think the Thread only works on "explicit" lists; the variables need to be expanded before being operated on.
After some experimentation, this works for me:
a = {a1, a2, a3};
b = {b1, b2, b3};
Thread[Set[Evaluate#a, Evaluate#b]];
{a1, a2, a3}
You could also write Thread[Evaluate#a = Evaluate#b]; just depends whichever you find more readable.
What's wrong with
MapThread[Set,{{a1,a2,a3},{b1,b2,b3}}]
?
Here's another solution:
a = {a1, a2, a3};
b = {b1, b2, b3};
each[{x_, y_}, Transpose[{a, b}],
x = y]
Which uses my handy each function:
SetAttributes[each, HoldAll]; (* each[pattern, list, body] *)
each[pat_, lst_, bod_] := (* converts pattern to body for *)
Scan[Replace[#, pat:>bod]&, Evaluate#lst] (* each element of list. *)
Similarly, you can do this:
MapThread[(#1 = #2)&, {a, b}]
Well, if they are really called a1, a2, etc, you could do this:
Assign[x_, y_] := Module[{s1, s2, n, sn},
s1 = SymbolName[Unevaluated[x]];
s2 = SymbolName[Unevaluated[y]];
For[n = 1, n <= Length[x] && n <= Length[y], n++,
sn = ToString[n];
Evaluate[Symbol[s1 <> sn]] = Evaluate[Symbol[s2 <> sn]]
]
]
SetAttributes[Assign, HoldAll]
And then
Clear[b1, b2, b3];
Clear[a1, a2, a3];
a = {a1, a2, a3}
b = {b1, b2, b3}
Assign[a, b]
a
Gives the results for a, b and a again as:
{a1, a2, a3}
{b1, b2, b3}
{b1, b2, b3}
As expected.
In general you can create expressions like these from proper use of SymbolName and Symbol, but be careful with your evaluation. If I had written SymbolName[x] (without the Unevaluated) then it would've interpreted that as SymbolName[{a1, a2, a3}], which is clearly not desirable. Not using Evaluate on Symbol[s1 <> sn] will have Mathematica complain that you're trying to reassign the Symbol function.
Related
Here's my solution for the water jugs problem
:- use_module(library(clpfd)).
initial(state(8, 0, 0)).
final(state(4, 4, 0)).
constraint(A0, A1, B0, B1, C0, C1, V) :-
A0 #< A1,
( B0 #> 0, T #= min(V - A0, B0), A1 #= A0 + T, B1 #= B0 - T, C1 #= C0
; C0 #> 0, T #= min(V - A0, C0), A1 #= A0 + T, C1 #= C0 - T, B1 #= B0
).
transition(state(A0, B0, C0), state(A1, B1, C1)) :-
( constraint(A0, A1, B0, B1, C0, C1, 8)
; constraint(B0, B1, A0, A1, C0, C1, 5)
; constraint(C0, C1, A0, A1, B0, B1, 3)
).
solve(A, A, _, [A]).
solve(A, B, P, [A|Q]) :-
transition(A, A1),
\+ member(A1, P),
solve(A1, B, [A|P], Q).
path(P) :-
initial(S0),
final(S),
solve(S0, S, [], P).
Is there a way to find the P of minimal length without traversing all options?
Here is a solution that makes more use of the power of clpfd: First state the problem, then try to solve it (using labeling/2 or similar). Given that we do not know the length of the (shortest) path, this will generate larger and larger problems until a solution is found. In my code, I do not prevent visiting the same state twice (but this could be added in the same way as in the MiniZinc model written by #DavidTonhofer, or as some post-processing). However, in order to ensure a finite search space, I've added code to stop the problem generation if the length of the path is longer than (5+1)*(3+1), as this is an upper bound on the number of different states (assuming we have do not add or remove water outside of the 3 jugs).
:- use_module(library(clpfd)).
initial(state(8, 0, 0)).
final(state(4, 4, 0)).
constraint(A0,A1,B0,B1,C0,C1,R,Max):-
T#=min(Max-B0,A0),
R in 0..1,
R#==>T#>0,
R#==>A1#=A0-T,
R#==>B1#=B0+T,
R#==>C1#=C0.
transition(state(A0, B0, C0), state(A1, B1, C1)) :-
A0+B0+C0#=A1+B1+C1,
A0 in 0..8,
B0 in 0..5,
C0 in 0..3,
A1 in 0..8,
B1 in 0..5,
C1 in 0..3,
constraint(A0,A1,B0,B1,C0,C1,RAB,5),
constraint(B0,B1,A0,A1,C0,C1,RBA,8),
constraint(A0,A1,C0,C1,B0,B1,RAC,3),
constraint(C0,C1,A0,A1,B0,B1,RCA,8),
constraint(C0,C1,B0,B1,A0,A1,RCB,5),
constraint(B0,B1,C0,C1,A0,A1,RBC,3),
RAB+RBA+RAC+RCA+RCB+RBC#=1.
solve(A, A, Xs, [A]):-
labeling([],Xs).
solve(A, B, Xs, [A|Q]) :-
length(Xs, L),
L < 24*3,
transition(A, A1),
A=state(X1,X2,X3),
solve(A1, B, [X1,X2,X3|Xs], Q).
path(P) :-
initial(S0),
final(S),
solve(S0, S, [], P).
I tried to keep the code relatively close to the one in the question. The main difference is that all the prolog-level disjunctions in transition/2 and constraint/7 have been removed and replaced by reification. In particular, I added the parameter R to constraint/8 which is equal to 1 if that specific transition is taken. Then I state in transition/2 that exactly one of the transitions must take place.
I must add that this formulation is not particularly efficient and I would not be surprised to find out that one can solve the problem more efficiently with either a different clpfd formulation or without using clpfd at all.
I am trying to understand DPLL procedure before actually coding it.
For example, I have these clauses:
C1 : {c, !d, !b}
C2 : {d, a}
C3: {b, !d, !a}
C4: {d, c, b, a}
C5: {c, !d, !b}
C6: {d, c, b}
C7: {c}
Now I take the decision variable as d = 0, b = 0. The clause looks like this now.
C1: {c, 1, 1}
C2: {a}
C3: {1, !a}
C4: {c, a}
C5: {c, 1, 1}
C6: {c}
C7: {c}
How do unit propagation and pure literal rule play a part here?
Also, in C3 : {1, !a} - when I take a = 1, then this becomes {1, 0}. What should be the final value for this clause? Should it be {1}?
And if any clause has value {!b}, that is a negation of a literal, after applying the decision variable, then how to proceed?
That step wouldn't have happened that way, because there were unit clauses in the input which would have been resolved first.
{ c } (the clause) is a unit and its literal c is positive, therefore c (the variable) is forced to be 1, then we have
C2 : {d, a}
C3: {b, !d, !a}
as active clauses, because true clauses are ignored.
Now b is a pure literal (it wasn't always, but it became one since some clauses are not active anymore), but practical SAT solvers usually don't check for that except during pre-processing since it can't be checked efficiently.
And finally you would set d or a or both it doesn't matter.
I'm new to Mathematica, and I'm trying to solve a matrix equation in a form as
AX = \lambda BX
Here, Aand B are 4*4 matrices in the following, \lambda is a value, Xis the eigenvector- 4*1 matrix.
A = {{a1 + b1, c, d, f},
{c, a2 + b2 , f , e},
{d , f , a3 + b1 , c},
{ f, e , c, a4 + b2}}
B = {{1, 0, 0 , 0},
{0, 1 , 0 , 0},
{0 , 0 , -1 , 0},
{0, 0 , 0, -1}}
I would like to solve this matrix equation and get the symbolical solution for \lambda using a1,a2,a3,a4,b1,b2,c,d,e,f, etc.
It would be much grateful if anyone can tell me.
Best regards,
mike
See Wolfram: Matrix Computations - specifically the section 'Generalized Eigenvalues'.
For n×n matrices A, B the generalized eigenvalues are the n
roots of its characteristic polynomial, p(𝛇) = det(A - 𝛇 B). For
each generalized eigenvalue, λ ∊ λ(A, B), the vectors, 𝛇, that
satisfy
A χ = λ B χ
are described as generalized eigenvectors.
Example using symbolic values:
matA = {{a11, a12}, {a21, a22}};
matB = {{b11, b12}, {b21, b22}};
Eigenvalues[{matA, matB}]
{(1/(2 (-b12 b21+b11 b22)))(a22 b11-a21 b12-a12 b21+a11 b22-Sqrt[(-a22 b11+a21 b12+a12 b21-a11 b22)^2-4 (-a12 a21+a11 a22) (-b12 b21+b11 b22)]),(1/(2 (-b12 b21+b11 b22)))(a22 b11-a21 b12-a12 b21+a11 b22+Sqrt[(-a22 b11+a21 b12+a12 b21-a11 b22)^2-4 (-a12 a21+a11 a22) (-b12 b21+b11 b22)])}
Eigenvectors[{matA, matB}]
...
So I have a simpler variation of the Einstein/Zebra puzzle in Prolog.
And I came up with this possible solution:
b_setval(T_age, Var).
friends(L) :-
L = [person(A1, B1, T_age), person(A2, B2, C2), person(A3, B3, T_age+3)],
:
:
member(person(_,yang,T_age+3),L),
member(person(_,_,18),L).
But my query friends(L). - false. only returns false as stated.
What am I doing wrong?
After following the answer of #luker, you can check your answer
friends(L) :-
% 1
L = [person(ada, _, Ta), person(ama, _, _), person(ana, _, _)],
% 2
member(person(_,_,15), L),
member(person(_,_,17), L),
member(person(_,_,18), L),
% 3
member(person(_, chang, _), L),
% 4
member(person(_, yang, Ty), L), Ty is Ta + 3,
% 5
member(person(_, thatcher, 17), L).
Interesting, this produces 2 results, which is weird for this kind of problem.
One potential problem that stands out is the T_age+3 term in the list L. In Prolog, this will not be arithmetically evaluated in-line. It will simply be the term, '+'(T_age,3). So the only element that would match this member of the list would be a term that looks like, person(X, Y, <something>+3). It's unclear whether this is your intention.
You can do a trace to see how variables are being instantiated with each member call, but let's try doing this manually for illustrative purposes:
L = [person(A1, B1, T_age), person(A2, B2, C2), person(A3, B3, T_age+3)],
member(person(ada, _,T_age),L),
...
This member call should succeed because Prolog can match it to person(A1, B1, T_age) in the list by unifying A1 = ada. The list L now looks like:
[person(ada, B1, T_age), person(A2, B2, C2), person(A3, B3, T_age+3)]
Moving on to the next member call:
member(person(ama, _, _),L),
...
This can't match the first member, but can match the second by unifying A2 = ama. L is now:
[person(ada, B1, T_age), person(ama, B2, C2), person(A3, B3, T_age+3)]
Then you have:
member(person(ana, _, _),L),
This can't match the first or second members, but can match the third by unifying A3 = ana. L is now:
[person(ada, B1, T_age), person(ama, B2, C2), person(ana, B3, T_age+3)]
The next member call is:
member(person(_,chang, _),L),
Which can match the first member again by unifying B1 = chang, so L becomes:
[person(ada, chang, T_age), person(ama, B2, C2), person(ana, B3, T_age+3)]
Then
member(person(_,yang,T_age+3),L),
This will match the second element of the list by unifying, B2 = yang and C2 = T_age+3. L then becomes:
[person(ada, chang, T_age), person(ama, yang, T_age+3), person(ana, B3, T_age+3)]
Then
member(person(_,thatcher,17),L),
This is where you have some trouble. It cannot match the first two elements of L because of the second argument. The third argument, 17 cannot match the term, T_age+3 in the third element of L. Remember: Prolog does not solve this as an equation T_age+3 = 17. It is just going to see 17 as an atomic integer, and see T_age+3 as a term with two arguments and find that they don't match. So this member call fails, and the whole predicate fails.
Hy everyones,
I've a little problem with a mathematica script which I need for fitting data points with a sum of 3 sine functions :
fit = NonlinearModelFit[Data,a1*Sin[b1*x + c1] + a2*Sin[b2*x + c2] + a3*Sin[b3*x + c3], {a1, b1,c1, a2, b2, c2, a3, b3, c3}, x]
I get this error :
NonlinearModelFit::cvmit: Failed to converge to the requested accuracy or precision within 100 iterations
I've tried with different starting values and with MaxIteration set to 10.000...
Maybe it's not the right way to do this kind of fitting. Does anyone have an idea about this?
Thanks!
Perhaps your data is too bad, but it works nicely with a good sample:
data = Table[{x, Sin[ x + .3] + 2 Sin[1.2 x] + 3 Sin[1.5 x + .5]},
{x, .01, 8 Pi, .001}];
fit = NonlinearModelFit[data,
a1*Sin[b1*x + c1] + a2*Sin[b2*x + c2] + a3*Sin[b3*x + c3],
{a1, b1, c1, a2, b2, c2, a3, b3, c3}, x]
Show[ListPlot[data], Plot[fit[x], {x, 0, 8 Pi}, PlotStyle -> Red], Frame -> True]