Create polyhedron for given matrix A and vector b - combinatorics

P(A,b)={x∣ A.x ≤ b}
I have to create a polyhedron with this inputs
but in sage inequalities like this A.x + b >= 0 , how to convert

Something like this:
sage: A = matrix(2,2,[1,23,31,4])
sage: b = vector([55,66])
sage: Polyhedron(ieqs=(-A).augment(b)).inequalities_list()
[[-1, -23, 55], [-31, -4, 66]]

Related

How to solve a cubic function without knowing the coefficients a b and c?

Apology for this silly question. In the cubie function below (as from a school project), I am given the value for x and f(x) while coefficients a, b ,c and constant of d are unknown.
f(x)=ax^3+bx^2+cx+d
In such case, is there a way to find out a, b and c by using any python package? I found good amount of python tutorial for solving cubic function but they seem to mainly focus on solving x while a, b and c value are given.
Here is an approach via sympy, Python's symbolic math library.
As an example, we are trying to find the formula for the sum of the first n triangular numbers. The triangular numbers (formula n*(n+1)/2) are 0, 1, 3, 6, 10, 15, 21, ..... The sums of the first n triangular numbers are thus 0, 1, 4, 10, 20, 35, 56, ....
from sympy import Eq, solve
from sympy.abc import a,b,c,d, x
formula = a*x**3 + b*x**2 + c*x + d # general cubic formula
xs = [0, 1, 2, 3] # some x values
fxs = [0, 1, 4, 10] # the corresponding function values
sol = solve([Eq(formula.subs(x, xi), fx) for xi, fx in zip(xs, fxs)])
print(sol) # {a: 1/6, b: 1/2, c: 1/3, d: 0}
You can use more x, fx pairs to check that a cubic formula suffices (this won't work with float values, as sympy needs exact symbolic equations).
Also sympy's interpolate can be interesting. This calculates a polynomial through some given points. Such code could look like:
from sympy import interpolate
from sympy.abc import x
xs = [0, 1, 2, 3]
fxs = [0, 1, 4, 10]
fx_dict = dict(zip(xs, fxs))
sol = interpolate(fx_dict, x)
print(sol) # x**3/6 + x**2/2 + x/3
print(sol.factor()) # x*(x + 1)*(x + 2)/6

How to do batched dot product in PyTorch?

I have a input tensor that is of size [B, N, 3] and I have a test tensor of size [N, 3] . I want to apply a dot product of the two tensors such that I get [B, N] basically. Is this actually possible?
Yes, it's possible:
a = torch.randn(5, 4, 3)
b = torch.randn(4, 3)
c = torch.einsum('ijk,jk->ij', a, b) # torch.Size([5, 4])
Another alternative:
a = torch.randn(5, 4, 3)
b = torch.randn(4, 3)
c = (a * b[None, ...]).sum(dim=-1) # torch.Size([5, 4])

Finding a vector that is orthogonal to n columns of a matrix

Given a matrix B with shape (M, N), where M > N. How to find a vector v (with shape of M) that is perpendicular to all columns in B.
I tried using Numpy numpy.linalg.lstsq method to solve : Bx = 0. 0 here is a vector with M zeros.
It returns a vector of zeros with (N,) shape.
You can use sympy library, like
from sympy import Matrix
B = [[2, 3, 5], [-4, 2, 3], [0, 0, 0]]
V = A.nullspace()[0]
or to find whole nullspace
N = A.nullspace()
Here is what worked for me in case someone else need the answer:
u,s,vh=np.linalg.svd(B)
v=vh[-1:1]

how to broadcast a sympy lambdify generated function?

I'd like to compute a Sympy expression as a function of two of the symbols in it. A function of 1 variable can readily be broadcast-ed after lambdifying it:
x, y, z = symbols('x y z')
expr = x*y + z
f = lambdify(x, expr.subs({z:2, y:4}))
x = np.linspace(1, 4, 5)
f(x)
But is there a way to use some builtin capability of numpy or sympy to broadcast higher dimensionally? In other words, is there a more direct or cleaner way to do the following?
x, y, z = symbols('x y z')
expr = x*y + z
f = lambdify([x, y], expr.subs({z:2}))
def g(xy):
k = xy.shape[1]
a = np.ndarray((k,k))
for j in range(k):
for i in range(k):
a[j, i] = f(xy[0, j], xy[1, i])
return a
x = np.linspace(0, 4, 5)
y = np.linspace(10, 12, 5)
xy = np.array([x,y])
g(xy)
You just need to get the shapes right for broadcasting:
In [11]: x, y, z = symbols('x y z')
...: expr = x*y + z
...: f = lambdify([x, y], expr.subs({z:2}))
In [12]: x = np.linspace(0, 4, 5).reshape((5, 1))
In [13]: y = np.linspace(10, 12, 5)
In [14]: f(x, y)
Out[14]:
array([[ 2. , 2. , 2. , 2. , 2. ],
[12. , 12.5, 13. , 13.5, 14. ],
[22. , 23. , 24. , 25. , 26. ],
[32. , 33.5, 35. , 36.5, 38. ],
[42. , 44. , 46. , 48. , 50. ]])
help(f) (or print(f.__doc__)) shows:
Help on function _lambdifygenerated:
_lambdifygenerated(x, y)
Created with lambdify. Signature:
func(x, y)
Expression:
x*y + 2
Source code:
def _lambdifygenerated(x, y):
return (x*y + 2)
The python/numpy function just does x*y+2. That's a simple translation of the sympy. Use standard numpy array broadcasting.
For example a (3,1) array with (2,) produces a (3,2) result:
In [35]: np.arange(3)[:,None] * np.arange(10,12) + 2
Out[35]:
array([[ 2, 2],
[12, 13],
[22, 24]])
In [36]: f(np.arange(3)[:,None], np.arange(10,12))
Out[36]:
array([[ 2, 2],
[12, 13],
[22, 24]])

How to create a subset list based upon another list values with Python

I have three identical length lists for scatter plotting: x (float), y (float) and c (integer for colour), and would like to split up the x and y lists into subsets filtered by the colour value so that I can use a legend the delineate them in a plot
While I could achieve this with len(c) loops over the x and y lists, it is not very pythonic, and I was hoping someone could provide something a bit more elegant
I was thinking something like the following, but it's clearly not working
c_list = list(set(c))
xset = []
yset = []
for j in c_list:
xset.append([i for i in x if j in c])
yset.append([i for i in y if j in c])
Be gentle - I've only been learning Python for a week or so!
Thanks in advance
I hope this helps:
x = [1, 2, 3, 4, 5]
y = [5, 3, 1, 3, 2]
c = [1, 3, 2, 3, 1]
c_list = list(set(c))
xset = []
yset = []
for j in c_list:
xset.append([x[i] for i, v in enumerate(c) if v == j])
yset.append([y[i] for i, v in enumerate(c) if v == j])
print(xset)
print(yset)

Resources