Python - Subtracting elements in lists of tuples - list

I have two lists of tuples.
x = [(A1, B1, C1), (A2, B2, C2),...(AN, BN, CN)]
and
y = [(A1_, B1_, C1_), (A2_, B2_, C2_),...(AN_, BN_, CN_)]
I want to do the following things:
Obtain a new list[(A1, B1, C1 - C1_), (A2, B2, C2 -
C2_),...(AN, BN, CN - CN_)]
And from there, create a list that
solely consists of [C1 - C1_, C2 - C2_,...]
I'd venture to say that something in Numpy would allow me to do this, but I still have not dug up how to just do an operation on one element in a tuple, so I would appreciate any possible help.
Thanks.

If you start with x and y being a list of tuples, then it is easy to convert them to 2D NumPy arrays:
import numpy as np
x = np.array([(1,2,3), (4,5,6), (7,8,9)])
y = np.array([(10,20,30), (40,50,60), (70,80,90)])
Then to create an array similar to
[(A1, B1, C1 - C1_), (A2, B2, C2 - C2_),...(AN, BN, CN - CN_)]
you could do this:
z = x[:] # make a copy of array x
z[:,2] -= y[:,2] # subtract the 3rd column of y from z
print(z)
yields
[[ 1 2 -27]
[ 4 5 -54]
[ 7 8 -81]]
and to get
[C1 - C1_, C2 - C2_,...]
you could either use z[:, 2] or obtain it directly from x and y using x[:, 2] - y[:, 2]:
[-27 -54 -81]
I might be misunderstanding your question, but when you say "I still have not dug up how to just do an operation on one element in a tuple" it makes me think you might be storing tuples in a NumPy array. If that's true, then I'd urge you to reconsider the way you are using NumPy:
You see, when you use dtype=object to store Python objects in a NumPy array (such as a tuple), then all operations done on these objects ultimately involve calls to Python functions, rather than the faster C/Fortran compiled functions that NumPy normally calls.
Thus, while you may enjoy NumPy syntax for selecting items in the array, you do not gain any speed advantage over plain Python objects. In fact, it can be slower than using plain Python objects (such as a list of tuples).
For this reason, I would recommend avoiding storing Python objects in NumPy arrays whenever possible, and especially when those objects are numerical, since NumPy's native numeric dtypes serve much better.
Instead of storing 3-tuples in an array, it would be better to add an extra dimension (a so-called "axis") to the NumPy array and store the 3 components along this axis.
Once you do that, the numerical calculation you contemplate is a piece of cake. It could be done with something like:
x[:,2]-y[:,2]

Without numpy:
>>> x = [(1,2,3), (4,5,6), (7,8,9)]
>>> y = [(10,20,30), (40,50,60), (70,80,90)]
>>> [ (a[0], a[1], a[2] - b[2]) for a, b in zip(x, y) ]
[(1, 2, -27), (4, 5, -54), (7, 8, -81)]
>>> [ a[2] - b[2] for a, b in zip(x, y) ]
[-27, -54, -81]

Related

Sympy IndexedBase substitution

I am a bit confused how to use Indexed objects in Sympy. Suppose I have the following setup:
from sympy import *
x = IndexedBase('x')
i = Idx('i')
s = Sum(x[i], (i, 0, 5))
s
Output:
5
___
╲
╲
╱ x[i]
╱
‾‾‾
i = 0
Which ofcourse is equal to
x[0] + x[1] + x[2] + x[3] + x[4] + x[5]
By doing s.doit(). Now, how do I substitute x with some range? I expected the following to work:
s.subs(x, list(range(6)))
But it does not do anything it would seem. However s.doit().subs(x[0], 0) works, but it will only substitute 1 element. Is it not intended to substitute IndexedBase with some list?
When using SymPy, there is one thing to always keep in mind: the only objects SymPy operates on is SymPy's object. Consider this example:
expr = x + 3
r = expr.subs(x, 2)
print(r, type(r))
5 <class 'sympy.core.numbers.Integer'>
Here, we passed a int number to subs, but SymPy internally converted it to an Integer number. Then, it performed the addition, producing the Integer number 5.
There are occasions in which the input parameter cannot be converted to something SymPy can understand. Your example is one such occasion. Let's use sympify (or its alias S) to verify how your list is going to be converted to a SymPy object:
l = list(range(6))
c = sympify(l)
type(c)
# out: list
As we can see, SymPy is unable to convert an object of type list, hence it is unable to use it. In short, this is the reason your code doesn't produce the correct output.
However, let's try the same trick with a tuple:
c = sympify(tuple(l))
type(c)
# out: Tuple
Here, SymPy converted a Python object of type tuple to a SymPy object of type Tuple. Now, the substitution should produce the correct result:
s.doit().subs(x, tuple(l))
# out: 15
Here are the most common SymPy objects the support iteration: Tuple, Matrix, Array.

Sorted() syntax difference between Python2.7 and Python 3.6.0, or erratum? [duplicate]

In Python 2, I can write:
In [5]: points = [ (1,2), (2,3)]
In [6]: min(points, key=lambda (x, y): (x*x + y*y))
Out[6]: (1, 2)
But that is not supported in 3.x:
File "<stdin>", line 1
min(points, key=lambda (x, y): (x*x + y*y))
^
SyntaxError: invalid syntax
The straightforward workaround is to index explicitly into the tuple that was passed:
>>> min(points, key=lambda p: p[0]*p[0] + p[1]*p[1])
(1, 2)
This is very ugly. If the lambda were a function, I could do
def some_name_to_think_of(p):
x, y = p
return x*x + y*y
But because the lambda only supports a single expression, it's not possible to put the x, y = p part into it.
How else can I work around this limitation?
No, there is no other way. You covered it all. The way to go would be to raise this issue on the Python ideas mailing list, but be prepared to argue a lot over there to gain some traction.
Actually, just not to say "there is no way out", a third way could be to implement one more level of lambda calling just to unfold the parameters - but that would be at once more inefficient and harder to read than your two suggestions:
min(points, key=lambda p: (lambda x,y: (x*x + y*y))(*p))
Python 3.8 update
Since the release of Python 3.8, PEP 572 — assignment expressions — have been available as a tool.
So, if one uses a trick to execute multiple expressions inside a lambda - I usually do that by creating a tuple and just returning the last component of it, it is possible to do the following:
>>> a = lambda p:(x:=p[0], y:=p[1], x ** 2 + y ** 2)[-1]
>>> a((3,4))
25
One should keep in mind that this kind of code will seldom be more readable or practical than having a full function. Still, there are possible uses - if there are various one-liners that would operate on this point, it could be worth to have a namedtuple, and use the assignment expression to effectively "cast" the incoming sequence to the namedtuple:
>>> from collections import namedtuple
>>> point = namedtuple("point", "x y")
>>> b = lambda s: (p:=point(*s), p.x ** 2 + p.y ** 2)[-1]
According to http://www.python.org/dev/peps/pep-3113/ tuple unpacking are gone, and 2to3 will translate them like so:
As tuple parameters are used by lambdas because of the single
expression limitation, they must also be supported. This is done by
having the expected sequence argument bound to a single parameter and
then indexing on that parameter:
lambda (x, y): x + y
will be translated into:
lambda x_y: x_y[0] + x_y[1]
Which is quite similar to your implementation.
I don't know any good general alternatives to the Python 2 arguments unpacking behaviour. Here's a couple of suggestion that might be useful in some cases:
if you can't think of a name; use the name of the keyword parameter:
def key(p): # more specific name would be better
x, y = p
return x**2 + y**3
result = min(points, key=key)
you could see if a namedtuple makes your code more readable if the list is used in multiple places:
from collections import namedtuple
from itertools import starmap
points = [ (1,2), (2,3)]
Point = namedtuple('Point', 'x y')
points = list(starmap(Point, points))
result = min(points, key=lambda p: p.x**2 + p.y**3)
While the destructuring arguments was removed in Python3, it was not removed from comprehensions. It is possible to abuse it to obtain similar behavior in Python 3.
For example:
points = [(1,2), (2,3)]
print(min(points, key=lambda y: next(x*x + y*y for (x,y) in [y])))
In comparison with the accepted answer of using a wrapper, this solution is able to completely destructure the arguments while the wrapper only destructures the first level. That is, you can do
values = [(('A',1),'a'), (('B',0),'b')]
print(min(values, key=lambda y: next(b for ((a,b),c) in (y,))))
In comparison to the accepted answer using an unwrapper lambda:
values = [(('A',1),'a'), (('B',0),'b')]
print(min(points, key=lambda p: (lambda a,b: (lambda x,y: (y))(*a))(*p)))
Alternatively one can also use a list instead of a tuple.
values = [(('A',1),'a'), (('B',0),'b')]
print(min(points, key=lambda y: next(b for (a,b),c in [y])))
This is just to suggest that it can be done, and should not be taken as a recommendation. However, IMO, this is better than the hack of using using multiple expressions in a tuple and returning the last one.
I think the better syntax is x * x + y * y let x, y = point, let keyword should be more carefully chosen.
The double lambda is the closest version.
lambda point: (lambda x, y: x * x + y * y)(*point)
High order function helper would be useful in case we give it a proper name.
def destruct_tuple(f):
return lambda args: f(*args)
destruct_tuple(lambda x, y: x * x + y * y)
Consider whether you need to unpack the tuple in the first place:
min(points, key=lambda p: sum(x**2 for x in p))
or whether you need to supply explicit names when unpacking:
min(points, key=lambda p: abs(complex(*p)**2)
Based on Cuadue suggestion and your comment on unpacking still being present in comprehensions, you can use, using numpy.argmin :
result = points[numpy.argmin(x*x + y*y for x, y in points)]
Another option is to write it into a generator producing a tuple where the key is the first element. Tuples are compared starting from beginning to end so the tuple with the smallest first element is returned. You can then index into the result to get the value.
min((x * x + y * y, (x, y)) for x, y in points)[1]
There may be a real solution to this, using PyFunctional!
Although not currently supported, I've submitted a tuple arg unpacking feature request to support:
(
seq((1, 2), (3, 4))
.map(unpack=lambda a, b: a + b)
) # => [3, 7]
Since questions on Stack Overflow are not supposed to contain the answer in the question, nor have explicit "update" sections, I am converting OP's original "updates" to a proper answer and making it community wiki.
OP originally claimed that this solution was "extending the idea in the answer". I cannot discern which answer that meant, or which idea. The idea is functionally the same as anthony.hl's answer, but that came years later. Considering the state of answers at the time, I think this qualifies as OP's original work.)
Make a wrapper function that generalizes the process of unpacking the arguments, like so:
def star(f):
return lambda args: f(*args)
Now we can use this to transform the lambda we want to write, into one that will receive the argument properly:
min(points, key=star(lambda x,y: (x*x + y*y))
We can further clean this up by using functools.wraps:
import functools
def star(f):
#functools.wraps(f)
def f_inner(args):
return f(*args)
return f_inner

Read array from input file and optimise variables Python

I have a simple Python code which reads two matrices in from a file, and each matrix element contains two undefined variables, A and B. After being read in, A and B are assigned values and the string is evaluated using eval() (I am aware of the security issues with this, but this code will only ever be used by me). A numpy array is formed for each of the matrices and the general eigenvalue problem is solved using both matrices. The code is as follows:
from __future__ import division
from scipy.linalg import eigh
import numpy as np
Mat_size = 4 ## Define matrix size
## Input the two matrices from each side of the generalised eigenvalue problem
HHfile = "Matrix_4x4HH.mat" ## left
SSfile = "Matrix_4x4SS.mat" ## right
## Read matrix files
f1 = open(HHfile, 'r')
HH_data = f1.read()
f2 = open(SSfile, 'r')
SS_data = f2.read()
# Define dictionary of variables
trans = {'A':2.1,
'B':3,
}
HHmat = eval(HH_data, trans)
SSmat = eval(SS_data, trans)
## Convert to numpy array
HHmat2 = np.array(HHmat)
SSmat2 = np.array(SSmat)
## Form correct matrix dimensions
shape = ( Mat_size, Mat_size )
HH = HHmat2.reshape( shape )
SS = SSmat2.reshape( shape )
ev, ew = eigh(HH,SS)
## solve eigenvalue problem
eigenValues,eigenVectors = eigh(HH,SS)
print("Eigenvalue:",eigenValues[0])
This works and outputs an answer. However I wish to vary the parameters A and B to minimise the value that it gives me. (Something like e.g.):
def func_min(x):
A, B = x
ev, ew=eigh(HH,SS)
return ev[0]
x0 = np.array([2.1, 3]) # Preliminary guess
minimize(func_min, x0)
I am having an issue with the fact that A and B have to be defined in order for the eval call to work; thus disallowing the optimisation routine as A and B cannot be varied since they have specified values. I have tried a few different methods of reading in the files (pandas, csv, np.genfromtxt etc...) but always seem to be left with the same issue. Is there a better way, or a modification to the current code to implement this?
Here are the inputs from the files. I was unsure of how to include them, but I can use a file upload service if that is better.
Input from Matrix_4x4HH.mat
-.7500000000000000/B**3*A**5/(A+B)**6-4.500000000000000/B**2*A**4/(A+B)**6-12./B*A**3/(A+B)**6-19.50000000000000*A**2/(A+B)**6-118.5000000000000*B*A/(A+B)**6-19.50000000000000*B**2/(A+B)**6-12.*B**3/A/(A+B)**6-4.500000000000000*B**4/A**2/(A+B)**6-.7500000000000000*B**5/A**3/(A+B)**6+2./B**3*A**4/(A+B)**6+13./B**2*A**3/(A+B)**6+36./B*A**2/(A+B)**6+165.*A/(A+B)**6+165.*B/(A+B)**6+36.*B**2/A/(A+B)**6+13.*B**3/A**2/(A+B)**6+2.*B**4/A**3/(A+B)**6,-.3750000000000000/B**3*A**5/(A+B)**6-2.125000000000000/B**2*A**4/(A+B)**6-4.750000000000000/B*A**3/(A+B)**6-23.87500000000000*A**2/(A+B)**6-1.750000000000000*B*A/(A+B)**6-23.87500000000000*B**2/(A+B)**6-4.750000000000000*B**3/A/(A+B)**6-2.125000000000000*B**4/A**2/(A+B)**6-.3750000000000000*B**5/A**3/(A+B)**6-1.500000000000000/B**2*A**3/(A+B)**6-5.500000000000000/B*A**2/(A+B)**6-25.*A/(A+B)**6-25.*B/(A+B)**6-5.500000000000000*B**2/A/(A+B)**6-1.500000000000000*B**3/A**2/(A+B)**6,1.500000000000000/B**3*A**6/(A+B)**7+10.12500000000000/B**2*A**5/(A+B)**7+29.12500000000000/B*A**4/(A+B)**7+45.25000000000000*A**3/(A+B)**7-135.1250000000000*B*A**2/(A+B)**7+298.7500000000000*B**2*A/(A+B)**7+14.87500000000000*B**3/(A+B)**7-5.750000000000000*B**4/A/(A+B)**7-2.375000000000000*B**5/A**2/(A+B)**7-.3750000000000000*B**6/A**3/(A+B)**7-4./B**3*A**5/(A+B)**7-27./B**2*A**4/(A+B)**7-76.50000000000000/B*A**3/(A+B)**7-9.500000000000000*A**2/(A+B)**7-305.*B*A/(A+B)**7-362.*B**2/(A+B)**7-14.50000000000000*B**3/A/(A+B)**7-1.500000000000000*B**4/A**2/(A+B)**7,-.3750000000000000/B**3*A**6/(A+B)**7-2.375000000000000/B**2*A**5/(A+B)**7-5.750000000000000/B*A**4/(A+B)**7+14.87500000000000*A**3/(A+B)**7+298.7500000000000*B*A**2/(A+B)**7-135.1250000000000*B**2*A/(A+B)**7+45.25000000000000*B**3/(A+B)**7+29.12500000000000*B**4/A/(A+B)**7+10.12500000000000*B**5/A**2/(A+B)**7+1.500000000000000*B**6/A**3/(A+B)**7-1.500000000000000/B**2*A**4/(A+B)**7-14.50000000000000/B*A**3/(A+B)**7-362.*A**2/(A+B)**7-305.*B*A/(A+B)**7-9.500000000000000*B**2/(A+B)**7-76.50000000000000*B**3/A/(A+B)**7-27.*B**4/A**2/(A+B)**7-4.*B**5/A**3/(A+B)**7,-.3750000000000000/B**3*A**5/(A+B)**6-2.125000000000000/B**2*A**4/(A+B)**6-4.750000000000000/B*A**3/(A+B)**6-23.87500000000000*A**2/(A+B)**6-1.750000000000000*B*A/(A+B)**6-23.87500000000000*B**2/(A+B)**6-4.750000000000000*B**3/A/(A+B)**6-2.125000000000000*B**4/A**2/(A+B)**6-.3750000000000000*B**5/A**3/(A+B)**6-1.500000000000000/B**2*A**3/(A+B)**6-5.500000000000000/B*A**2/(A+B)**6-25.*A/(A+B)**6-25.*B/(A+B)**6-5.500000000000000*B**2/A/(A+B)**6-1.500000000000000*B**3/A**2/(A+B)**6,-1.500000000000000/B**3*A**5/(A+B)**6-10.50000000000000/B**2*A**4/(A+B)**6-35./B*A**3/(A+B)**6-101.5000000000000*A**2/(A+B)**6-343.*B*A/(A+B)**6-101.5000000000000*B**2/(A+B)**6-35.*B**3/A/(A+B)**6-10.50000000000000*B**4/A**2/(A+B)**6-1.500000000000000*B**5/A**3/(A+B)**6+2./B**3*A**4/(A+B)**6+16./B**2*A**3/(A+B)**6+45./B*A**2/(A+B)**6+201.*A/(A+B)**6+201.*B/(A+B)**6+45.*B**2/A/(A+B)**6+16.*B**3/A**2/(A+B)**6+2.*B**4/A**3/(A+B)**6,.7500000000000000/B**3*A**6/(A+B)**7+5.625000000000000/B**2*A**5/(A+B)**7+18.87500000000000/B*A**4/(A+B)**7+19.62500000000000*A**3/(A+B)**7+170.3750000000000*B*A**2/(A+B)**7+73.87500000000000*B**2*A/(A+B)**7+57.62500000000000*B**3/(A+B)**7+4.875000000000000*B**4/A/(A+B)**7+.3750000000000000*B**5/A**2/(A+B)**7+1.500000000000000/B**2*A**4/(A+B)**7+7.500000000000000/B*A**3/(A+B)**7-1.*A**2/(A+B)**7+39.*B*A/(A+B)**7+47.50000000000000*B**2/(A+B)**7+1.500000000000000*B**3/A/(A+B)**7,.3750000000000000/B**2*A**5/(A+B)**7+4.875000000000000/B*A**4/(A+B)**7+57.62500000000000*A**3/(A+B)**7+73.87500000000000*B*A**2/(A+B)**7+170.3750000000000*B**2*A/(A+B)**7+19.62500000000000*B**3/(A+B)**7+18.87500000000000*B**4/A/(A+B)**7+5.625000000000000*B**5/A**2/(A+B)**7+.7500000000000000*B**6/A**3/(A+B)**7+1.500000000000000/B*A**3/(A+B)**7+47.50000000000000*A**2/(A+B)**7+39.*B*A/(A+B)**7-1.*B**2/(A+B)**7+7.500000000000000*B**3/A/(A+B)**7+1.500000000000000*B**4/A**2/(A+B)**7,1.500000000000000/B**3*A**6/(A+B)**7+10.12500000000000/B**2*A**5/(A+B)**7+29.12500000000000/B*A**4/(A+B)**7+45.25000000000000*A**3/(A+B)**7-135.1250000000000*B*A**2/(A+B)**7+298.7500000000000*B**2*A/(A+B)**7+14.87500000000000*B**3/(A+B)**7-5.750000000000000*B**4/A/(A+B)**7-2.375000000000000*B**5/A**2/(A+B)**7-.3750000000000000*B**6/A**3/(A+B)**7-4./B**3*A**5/(A+B)**7-27./B**2*A**4/(A+B)**7-76.50000000000000/B*A**3/(A+B)**7-9.500000000000000*A**2/(A+B)**7-305.*B*A/(A+B)**7-362.*B**2/(A+B)**7-14.50000000000000*B**3/A/(A+B)**7-1.500000000000000*B**4/A**2/(A+B)**7,.7500000000000000/B**3*A**6/(A+B)**7+5.625000000000000/B**2*A**5/(A+B)**7+18.87500000000000/B*A**4/(A+B)**7+19.62500000000000*A**3/(A+B)**7+170.3750000000000*B*A**2/(A+B)**7+73.87500000000000*B**2*A/(A+B)**7+57.62500000000000*B**3/(A+B)**7+4.875000000000000*B**4/A/(A+B)**7+.3750000000000000*B**5/A**2/(A+B)**7+1.500000000000000/B**2*A**4/(A+B)**7+7.500000000000000/B*A**3/(A+B)**7-1.*A**2/(A+B)**7+39.*B*A/(A+B)**7+47.50000000000000*B**2/(A+B)**7+1.500000000000000*B**3/A/(A+B)**7,-5.250000000000000/B**3/(A+B)**8*A**7-40.50000000000000/B**2/(A+B)**8*A**6-138.7500000000000/B/(A+B)**8*A**5-280.7500000000000/(A+B)**8*A**4-629.7500000000000*B/(A+B)**8*A**3+770.2500000000000*B**2/(A+B)**8*A**2-836.7500000000000*B**3/(A+B)**8*A-180.2500000000000*B**4/(A+B)**8-52.*B**5/(A+B)**8/A-12.75000000000000*B**6/(A+B)**8/A**2-1.500000000000000*B**7/(A+B)**8/A**3+14./B**3/(A+B)**8*A**6+107./B**2/(A+B)**8*A**5+355./B/(A+B)**8*A**4+778./(A+B)**8*A**3+284.*B/(A+B)**8*A**2+597.*B**2/(A+B)**8*A+911.*B**3/(A+B)**8+100.*B**4/(A+B)**8/A+20.*B**5/(A+B)**8/A**2+2.*B**6/(A+B)**8/A**3,.7500000000000000/B**3/(A+B)**8*A**7+5.625000000000000/B**2/(A+B)**8*A**6+18.25000000000000/B/(A+B)**8*A**5+52.50000000000000/(A+B)**8*A**4+397.*B/(A+B)**8*A**3-2356.250000000000*B**2/(A+B)**8*A**2+397.*B**3/(A+B)**8*A+52.50000000000000*B**4/(A+B)**8+18.25000000000000*B**5/(A+B)**8/A+5.625000000000000*B**6/(A+B)**8/A**2+.7500000000000000*B**7/(A+B)**8/A**3+1.500000000000000/B**2/(A+B)**8*A**5+10.50000000000000/B/(A+B)**8*A**4-276.5000000000000/(A+B)**8*A**3+1848.500000000000*B/(A+B)**8*A**2+1848.500000000000*B**2/(A+B)**8*A-276.5000000000000*B**3/(A+B)**8+10.50000000000000*B**4/(A+B)**8/A+1.500000000000000*B**5/(A+B)**8/A**2,-.3750000000000000/B**3*A**6/(A+B)**7-2.375000000000000/B**2*A**5/(A+B)**7-5.750000000000000/B*A**4/(A+B)**7+14.87500000000000*A**3/(A+B)**7+298.7500000000000*B*A**2/(A+B)**7-135.1250000000000*B**2*A/(A+B)**7+45.25000000000000*B**3/(A+B)**7+29.12500000000000*B**4/A/(A+B)**7+10.12500000000000*B**5/A**2/(A+B)**7+1.500000000000000*B**6/A**3/(A+B)**7-1.500000000000000/B**2*A**4/(A+B)**7-14.50000000000000/B*A**3/(A+B)**7-362.*A**2/(A+B)**7-305.*B*A/(A+B)**7-9.500000000000000*B**2/(A+B)**7-76.50000000000000*B**3/A/(A+B)**7-27.*B**4/A**2/(A+B)**7-4.*B**5/A**3/(A+B)**7,.3750000000000000/B**2*A**5/(A+B)**7+4.875000000000000/B*A**4/(A+B)**7+57.62500000000000*A**3/(A+B)**7+73.87500000000000*B*A**2/(A+B)**7+170.3750000000000*B**2*A/(A+B)**7+19.62500000000000*B**3/(A+B)**7+18.87500000000000*B**4/A/(A+B)**7+5.625000000000000*B**5/A**2/(A+B)**7+.7500000000000000*B**6/A**3/(A+B)**7+1.500000000000000/B*A**3/(A+B)**7+47.50000000000000*A**2/(A+B)**7+39.*B*A/(A+B)**7-1.*B**2/(A+B)**7+7.500000000000000*B**3/A/(A+B)**7+1.500000000000000*B**4/A**2/(A+B)**7,.7500000000000000/B**3/(A+B)**8*A**7+5.625000000000000/B**2/(A+B)**8*A**6+18.25000000000000/B/(A+B)**8*A**5+52.50000000000000/(A+B)**8*A**4+397.*B/(A+B)**8*A**3-2356.250000000000*B**2/(A+B)**8*A**2+397.*B**3/(A+B)**8*A+52.50000000000000*B**4/(A+B)**8+18.25000000000000*B**5/(A+B)**8/A+5.625000000000000*B**6/(A+B)**8/A**2+.7500000000000000*B**7/(A+B)**8/A**3+1.500000000000000/B**2/(A+B)**8*A**5+10.50000000000000/B/(A+B)**8*A**4-276.5000000000000/(A+B)**8*A**3+1848.500000000000*B/(A+B)**8*A**2+1848.500000000000*B**2/(A+B)**8*A-276.5000000000000*B**3/(A+B)**8+10.50000000000000*B**4/(A+B)**8/A+1.500000000000000*B**5/(A+B)**8/A**2,-1.500000000000000/B**3/(A+B)**8*A**7-12.75000000000000/B**2/(A+B)**8*A**6-52./B/(A+B)**8*A**5-180.2500000000000/(A+B)**8*A**4-836.7500000000000*B/(A+B)**8*A**3+770.2500000000000*B**2/(A+B)**8*A**2-629.7500000000000*B**3/(A+B)**8*A-280.7500000000000*B**4/(A+B)**8-138.7500000000000*B**5/(A+B)**8/A-40.50000000000000*B**6/(A+B)**8/A**2-5.250000000000000*B**7/(A+B)**8/A**3+2./B**3/(A+B)**8*A**6+20./B**2/(A+B)**8*A**5+100./B/(A+B)**8*A**4+911./(A+B)**8*A**3+597.*B/(A+B)**8*A**2+284.*B**2/(A+B)**8*A+778.*B**3/(A+B)**8+355.*B**4/(A+B)**8/A+107.*B**5/(A+B)**8/A**2+14.*B**6/(A+B)**8/A**3
Input from Matrix_4x4SS.mat
1./B**3*A**3/(A+B)**6+6./B**2*A**2/(A+B)**6+15./B*A/(A+B)**6+84./(A+B)**6+15.*B/A/(A+B)**6+6.*B**2/A**2/(A+B)**6+1.*B**3/A**3/(A+B)**6,-.5000000000000000/B**3*A**3/(A+B)**6-3.500000000000000/B**2*A**2/(A+B)**6-9.500000000000000/B*A/(A+B)**6-53./(A+B)**6-9.500000000000000*B/A/(A+B)**6-3.500000000000000*B**2/A**2/(A+B)**6-.5000000000000000*B**3/A**3/(A+B)**6,-2./B**3*A**4/(A+B)**7-12.50000000000000/B**2*A**3/(A+B)**7-32.50000000000000/B*A**2/(A+B)**7+18.50000000000000*A/(A+B)**7-253.*B/(A+B)**7-17.50000000000000*B**2/A/(A+B)**7-4.500000000000000*B**3/A**2/(A+B)**7-.5000000000000000*B**4/A**3/(A+B)**7,-.5000000000000000/B**3*A**4/(A+B)**7-4.500000000000000/B**2*A**3/(A+B)**7-17.50000000000000/B*A**2/(A+B)**7-253.*A/(A+B)**7+18.50000000000000*B/(A+B)**7-32.50000000000000*B**2/A/(A+B)**7-12.50000000000000*B**3/A**2/(A+B)**7-2.*B**4/A**3/(A+B)**7,-.5000000000000000/B**3*A**3/(A+B)**6-3.500000000000000/B**2*A**2/(A+B)**6-9.500000000000000/B*A/(A+B)**6-53./(A+B)**6-9.500000000000000*B/A/(A+B)**6-3.500000000000000*B**2/A**2/(A+B)**6-.5000000000000000*B**3/A**3/(A+B)**6,2./B**3*A**3/(A+B)**6+14./B**2*A**2/(A+B)**6+38./B*A/(A+B)**6+212./(A+B)**6+38.*B/A/(A+B)**6+14.*B**2/A**2/(A+B)**6+2.*B**3/A**3/(A+B)**6,1./B**3*A**4/(A+B)**7+6.500000000000000/B**2*A**3/(A+B)**7+16.50000000000000/B*A**2/(A+B)**7-19.*A/(A+B)**7+118.*B/(A+B)**7+4.500000000000000*B**2/A/(A+B)**7+.5000000000000000*B**3/A**2/(A+B)**7,.5000000000000000/B**2*A**3/(A+B)**7+4.500000000000000/B*A**2/(A+B)**7+118.*A/(A+B)**7-19.*B/(A+B)**7+16.50000000000000*B**2/A/(A+B)**7+6.500000000000000*B**3/A**2/(A+B)**7+1.*B**4/A**3/(A+B)**7,-2./B**3*A**4/(A+B)**7-12.50000000000000/B**2*A**3/(A+B)**7-32.50000000000000/B*A**2/(A+B)**7+18.50000000000000*A/(A+B)**7-253.*B/(A+B)**7-17.50000000000000*B**2/A/(A+B)**7-4.500000000000000*B**3/A**2/(A+B)**7-.5000000000000000*B**4/A**3/(A+B)**7,1./B**3*A**4/(A+B)**7+6.500000000000000/B**2*A**3/(A+B)**7+16.50000000000000/B*A**2/(A+B)**7-19.*A/(A+B)**7+118.*B/(A+B)**7+4.500000000000000*B**2/A/(A+B)**7+.5000000000000000*B**3/A**2/(A+B)**7,7./B**3/(A+B)**8*A**5+50./B**2/(A+B)**8*A**4+154./B/(A+B)**8*A**3+331./(A+B)**8*A**2-147.*B/(A+B)**8*A+848.*B**2/(A+B)**8+80.*B**3/(A+B)**8/A+19.*B**4/(A+B)**8/A**2+2.*B**5/(A+B)**8/A**3,1./B**3/(A+B)**8*A**5+8.500000000000000/B**2/(A+B)**8*A**4+31./B/(A+B)**8*A**3-152.5000000000000/(A+B)**8*A**2+1568.*B/(A+B)**8*A-152.5000000000000*B**2/(A+B)**8+31.*B**3/(A+B)**8/A+8.500000000000000*B**4/(A+B)**8/A**2+1.*B**5/(A+B)**8/A**3,-.5000000000000000/B**3*A**4/(A+B)**7-4.500000000000000/B**2*A**3/(A+B)**7-17.50000000000000/B*A**2/(A+B)**7-253.*A/(A+B)**7+18.50000000000000*B/(A+B)**7-32.50000000000000*B**2/A/(A+B)**7-12.50000000000000*B**3/A**2/(A+B)**7-2.*B**4/A**3/(A+B)**7,.5000000000000000/B**2*A**3/(A+B)**7+4.500000000000000/B*A**2/(A+B)**7+118.*A/(A+B)**7-19.*B/(A+B)**7+16.50000000000000*B**2/A/(A+B)**7+6.500000000000000*B**3/A**2/(A+B)**7+1.*B**4/A**3/(A+B)**7,1./B**3/(A+B)**8*A**5+8.500000000000000/B**2/(A+B)**8*A**4+31./B/(A+B)**8*A**3-152.5000000000000/(A+B)**8*A**2+1568.*B/(A+B)**8*A-152.5000000000000*B**2/(A+B)**8+31.*B**3/(A+B)**8/A+8.500000000000000*B**4/(A+B)**8/A**2+1.*B**5/(A+B)**8/A**3,2./B**3/(A+B)**8*A**5+19./B**2/(A+B)**8*A**4+80./B/(A+B)**8*A**3+848./(A+B)**8*A**2-147.*B/(A+B)**8*A+331.*B**2/(A+B)**8+154.*B**3/(A+B)**8/A+50.*B**4/(A+B)**8/A**2+7.*B**5/(A+B)**8/A**3
Edit
I guess the important part of this question is how to read in an array of equations from a file into a numpy array and be able to assign values to the variables without the need for eval. As an example of what can usually be done using numpy:
import numpy as np
A = 1
B = 1
arr = np.array([[A+2*B,2],[2,B+6*(B+A)]])
print arr
This gives:
[[ 3 2]
[ 2 13]]
The numpy array contained algebraic terms and when printed the specified values of A and B were inserted. Is this possible with equations read from a file into a numpy array? I have looked through the numpy documentation and is the issue to do with the type of data being read in? i.e. I cannot specify the dtype to be int or float as each array element contains ints, floats and text. I feel there is a very simple aspect of Python I am missing that can do this.

Python: Solving equation system (coefficients are arrays)

I can solve a system equation (using NumPY) like this:
>>> a = np.array([[3,1], [1,2]])
>>> b = np.array([9,8])
>>> y = np.linalg.solve(a, b)
>>> y
array([ 2., 3.])
But, if I got something like this:
>>> x = np.linspace(1,10)
>>> a = np.array([[3*x,1-x], [1/x,2]])
>>> b = np.array([x**2,8*x])
>>> y = np.linalg.solve(a, b)
It doesnt work, where the matrix's coefficients are arrays and I want calculate the array solution "y" for each element of the array "x". Also, I cant calculate
>>> det(a)
The question is: How can do that?
Check out the docs page. If you want to solve multiple systems of linear equations you can send in multiple arrays but they have to have shape (N,M,M). That will be considered a stack of N MxM arrays. A quote from the docs page below,
Several of the linear algebra routines listed above are able to compute results for several matrices at once, if they are stacked into the same array. This is indicated in the documentation via input parameter specifications such as a : (..., M, M) array_like. This means that if for instance given an input array a.shape == (N, M, M), it is interpreted as a “stack” of N matrices, each of size M-by-M. Similar specification applies to return values, for instance the determinant has det : (...) and will in this case return an array of shape det(a).shape == (N,). This generalizes to linear algebra operations on higher-dimensional arrays: the last 1 or 2 dimensions of a multidimensional array are interpreted as vectors or matrices, as appropriate for each operation.
When I run your code I get,
>>> a.shape
(2, 2)
>>> b.shape
(2, 50)
Not sure exactly what problem you're trying to solve, but you need to rethink your inputs. You want a to have shape (N,M,M) and b to have shape (N,M). You will then get back an array of shape (N,M) (i.e. N solution vectors).

Euclidean algorithm on Sage for more than 2 elements

I'm trying to make an exercise which gets a list of numers, an shows a list of elements like this: if A=[a0,a1,a2] then there is U=[u0,u1,u2], knowing that a0*u0 + a1*u1 + a2*u2 = d and d is the gcd of A.
For 2 elements is a pretty simple thing, as Sage has a function to retrieve u0 and u1 out of a0 and a1:
A=[15,21]
(d,u0,u1)=xgcd(a[0],a[1])
I just don't understand how could I do this with a list of n elements.
Note that gcd(a, b, c) = gcd((gcd(a, b), c). This means that you can use the built-in function repeatedly to calculate the coefficients that you want.
You helped me a lot, came to this:
x1=[1256,5468,5552,1465]
n=-1
for i in x1:
n=n+1
(d,w,x)=xgcd(x1[n-1],x1[n])
u1=[w,x]
n=n-2
while n>=0:
div=d
(d,u,v)=xgcd(x1[n],div)
position=0
for j in u1:
a=j*v
u1[position]=a
position=position+1
u1=[u]+u1
n=n-1
u1
And it works ;)