Does anyone know how I can rewrite all sympy equations to have zero right hand side?
I have a list of equations, each a string, that I would like to feed into "linear_eq_to_matrix".
My list looks somehting like
eqs = ['x+y =2', 'x = y']
In order to use "linear_eq_to_matrix", I need 2 things:
Reformulate the equations to have zero right hand side.
Change the string format of the equations. This can be done with sympy.sympify.
My code needs to look something like
eqns = [x + y - 2,
x - y]
A, b = linear_eq_to_matrix(eqns, [x, y])
I am not sure how to perform 1) above in order to get "eqns".
edit: I found out how to transform a string to a sympy expression.
I think I solved it:
eqsString = ['x+y =2', 'x = y']
eqs = [sym.Eq(*map(sym.sympify, eq.split('='))) for eq in eqsString]
x, y = symbols('x, y')
A, b = linear_eq_to_matrix(eqs, [x, y])
A, b
Which gives
(Matrix([
[1, 1],
[1, -1]]), Matrix([
[2],
[0]]))
Related
I am trying to use zip in a pythonic way but in Julia. Given two lists:
a =[2;3;4;5;6]
b =[0;7;8;9;10]
I would like to create the following list comprehension,
c = [x for (x,y) in zip(a, b) if (x<y) else y]
that returns c = [0;3;4;5;6]. Instead I get syntax: expected "]" returned.
You have to rewrite your comprehension such that the condition is in the generator's "body":
c = [x < y ? x : y for (x, y) in zip(a, b)]
The if-condition in comprehensions is purely for filtering at the moment (although it might be possible to add the meaning you want).
So i found a way of solving it from stackoverflow and it involves this answer:
last(X,Y) :-
append(_,[X],Y).
But i can't actually understand how this actually works.
If anyone can help me it would be really helpful.Thanks.
You can use append/3 [swi-doc] in several directions. You can for example pass a list, and look how two lists can append to that list. For example:
?- append(X, Y, [1,4,2,5]).
X = [],
Y = [1, 4, 2, 5] ;
X = [1],
Y = [4, 2, 5] ;
X = [1, 4],
Y = [2, 5] ;
X = [1, 4, 2],
Y = [5] ;
X = [1, 4, 2, 5],
Y = [] ;
false.
As you can see, there are five ways to construct that. For example with X = [] and Y = [1,4,2,5], or with X = [1] and Y = [4,2,5].
We thus define the predicate last/2 as:
last(X, L) :-
append(_, [X], L).
Notice the [X] as second parameter. We here thus specify that the second list should be a singleton list (a list with exactly one element). An empty list, or a list with two or more elements will not unify with [X].
The append/3 predicate will this aim to unify the second list with candidates like we have seen in the example. But only if the second list is an singleton list, it will match, in which case X is unified with the last element.
See the definition on the SWI-Prolog website.
One of the examples is:
?- append(X, [Last], [a,b,c]).
X = [a,b],
Last = c.
It means the Last is the single element in a list.
Think of the imperative way that X appends the "Last" to the end of the list. Then, it becomes the list [a,b,c].
Therefore, to define the last, we could:
mylast(Xs,Last):-
append(_,[Last],Xs). % doesn't care about the rest of the elements except the [Last]
split(L,X,Y):-append(X,Y,L).
creates 4 splits as follows:
X = [],
Y = [1, 2, 3] ;
X = [1],
Y = [2, 3] ;
X = [1, 2],
Y = [3] ;
X = [1, 2, 3],
Y = [] ;
I want to eliminate the empty list created during split and keep only combinations which do not have empty list that is
X = [1],
Y = [2, 3] ;
X = [1, 2],
Y = [3] ;
You can first specify the pattern for X and Y, by unifying these with a "cons":
split(L, X, Y) :-
X = [_|_],
Y = [_|_],
append(X, Y, L).
The advantage of using this approach, is that you will probably safe some cycles, since append/3 will not propose certain solutions that are empty lists, that then have to be filtered out.
In order to solve your problem, as said in the comments, you have to add a condition that checks if X or Y are empty, in this way:
split(L,X,Y):-
append(X,Y,L),
x\=[],
Y\=[].
Why the check is done after append/3? X = [] is true because X can be unified with [] when it is still uninstantiated. When you call split/3, initially X and Y are uninstantiated (if you use the tracer you can see something like _4604\=[]): X = [] succeds and so the negation fails and the program returns false if you put X\=[] and Y\=[] before append/3.
To better understand, i suggest you to read this article.
I would like to create a function in Prolog that multiplies two lists together component-wise and returns the resulting list.
For example:
?- multiply_lists([1,2,3], [4,5,6], X).
X = [4,10,18].
I would like to write this function without using built-in Prolog functions.
First define the base case - the result of multiplying two empty lists together is the empty list.
multl([],[],[]).
Then, define the recursive part of the function:
multl([H1|Tail1],[H2|Tail2], [H3|Tail3]):-
multl(Tail1, Tail2, Tail3),
H3 is (H1 * H2).
What's happening here is that the elements at the head of both lists are being multiplied together and being concatenated with the tail of the result.
All in all, we have:
multl([],[],[]).
multl([H1|Tail1],[H2|Tail2], [H3|Tail3]):-
multl(Tail1, Tail2, Tail3),
H3 is (H1 * H2).
Interesting is
:- use_module(library(clpfd)).
mutiply(X,Y,Z) :-
Z #= X*Y.
multiply_lists(L1, L2, Out) :-
maplist(mutiply, L1,L2,Out).
With results :
?- multiply_lists([1,2,3], [4,5,6], X).
X = [4, 10, 18].
?- multiply_lists(X, [4,5,6], [4,8,10]).
false.
?- multiply_lists(X, [4,5,6], [4,10,18]).
X = [1, 2, 3].
?- multiply_lists(X, Y, [4,10,18]).
X = [_17366, _17372, _17378],
Y = [_17396, _17402, _17408],
_17366 in -4.. -1\/1..4,
_17366*_17396#=4,
_17396 in -4.. -1\/1..4,
_17372 in -10.. -1\/1..10,
_17372*_17402#=10,
_17402 in -10.. -1\/1..10,
_17378 in -18.. -1\/1..18,
_17378*_17408#=18,
_17408 in -18.. -1\/1..18.
I am new in haskell and i got an exercise. In theory i know how i should to do it, but i dont know how can i do it in Haskell. My exercise is create a list generator which generate the following infinite list : [1,-1,3,-3,5,-5..]. I would like to do it with two different sequence.
Is there any way to solve this problem something like that:
[ something [a,b,a,b..] | a<-[1,3..], b<-[-1,-3..] ]
I have already tried to search the solution, but I couldn't find any about list generator.
I'll give you an hint.
A list generator of the form:
[f x y | x <- xs, y <- ys]
Will perform the cartesian product of lists xs and ys (well, it will apply f to each element of the product). Assume xs = [1,2,3] and ys = ['a', 'b'] the above list-comprehension is equivalent to:
[f 1 'a', f 1 'b', f 2 'a', f 2 'b', f 3 'a', f 3 'b']
so for each element x of xs the whole ys is iterated over thus producing all the f x y0, f x y1, ..., f x yN values.
Ask yourself:
what happens when ys is infinite?
Hence is your approach in anyway feasible with that structure?
Can you think of an other technique of generating 1 and -1, then 3 and -3 which doesn't require two infinite lists but only one infinite list?
Now try hard to answer these questions yourself, since there's no point in doing exercises if not for actually exercising your mind. I'll provide the answer in spoiler quotes so that if you give up you can have a possible solution:
[x*y | x <- [1, 3..], y <- [1,-1]]