SymPy : creating a numpy function from diagonal matrix that takes a numpy array
Asked Answered
D

2

11

Building on an example I've found here, I am trying to create a function from a diagonal matrix that was created using sumpy.diag

myM = Matrix([
[x1, 4, 4],
[4, x2, 4],
[4, 4, x3]])  

Where this was created using this routine for example:

import sympy as sp
import numpy as np

x1 = sp.Symbol('x1')
x2 = sp.Symbol('x2')
x3 = sp.Symbol('x3')
X = sp.Matrix([x1, x2, x3])

myM = 4 * sp.ones(3, 3)
sp.diag(*X) + myM - sp.diag(*np.diag(myM))

now I will like to create a function, using lambdify of ufuncify, that takes a numpy.array or length 3 (like np.array([0.1,0.2,0.3]))as an input, and gives the output as a matrix according to myM

myM = Matrix([
[0.1, 4, 4],
[4, 0.2, 4],
[4, 4, 0.3]])  

Eventually I need to create a Jacobian matrix symbolically using this method: Jacobian And as the functional form may change during the calculation, then having the Jacobian calculated symbolically would be very useful.

Disgusting answered 22/1, 2017 at 13:24 Comment(0)
H
9

The creation of a numeric 3 by 3 matrix from a numeric vector is not really a SymPy thing, since no symbols are involved. Consider the following, where the argument d is an array holding the diagonal elements.

def mat(d):
    return np.diag(d-4) + 4

The above function returns a 2d NumPy array. To return a SymPy matrix instead, use

def mat(d):
    return sp.Matrix(np.diag(d-4) + 4)

When d has extremely small values, the subtraction followed by addition may cause loss of precision: for example, (1e-20 - 4) + 4 evaluates to zero. A safer alternative is

def mat(d):
    diagmat = np.diag(d) 
    return diagmat + np.fromfunction(lambda i, j: (i != j)*4, diagmat.shape)
Hurl answered 22/1, 2017 at 17:9 Comment(1)
Not exactly what I had in mind, but I'll use it, so thanksDisgusting
S
5

you can .subs() float values into the respective symbols:

import sympy as sp
import numpy as np

x1 = sp.Symbol('x1')
x2 = sp.Symbol('x2')
x3 = sp.Symbol('x3')
X = sp.Matrix([x1, x2, x3])

myM = 4 * sp.ones(3, 3)
smyM=sp.diag(*X) + myM - sp.diag(*np.diag(myM))

fcoefs = [(a, f) for a, f in (zip([x1, x2, x3], np.array([0.1,0.2,0.3])))]

fmyM = smyM.subs(fcoefs)

smyM
Out[105]: 
Matrix([
[x1,  4,  4],
[ 4, x2,  4],
[ 4,  4, x3]])

fmyM
Out[106]: 
Matrix([
[0.1,   4,   4],
[  4, 0.2,   4],
[  4,   4, 0.3]])

seems to be a fine sympy.matrices.dense.MutableDenseMatrix Matrix after:

fmyM @ myM
Out[107]: 
Matrix([
[32.4, 32.4, 32.4],
[32.8, 32.8, 32.8],
[33.2, 33.2, 33.2]])

may need conversion to a np.array for full use with numpy

below is some of my code showing more of the pattern I used:

def ysolv(coeffs):
    x,y,a,b,c,d,e = symbols('x y a b c d e')
    ellipse = a*y**2 + b*x*y + c*x + d*y + e - x**2
    y_sols = solve(ellipse, y)
    print(*y_sols, sep='\n')

    num_coefs = [(a, f) for a, f in (zip([a,b,c,d,e], coeffs))]
    y_solsf0 = y_sols[0].subs(num_coefs)
    y_solsf1 = y_sols[1].subs(num_coefs)

    f0 = lambdify([x], y_solsf0)
    f1 = lambdify([x], y_solsf1)
    return f0, f1

f0, f1 = ysolv(t[0])

y0 = [f0(x) for x in xs]
y1 = [f1(x) for x in xs]
...    

from: https://mcmap.net/q/1017280/-drawing-elliptical-orbit-in-python-using-numpy-matplotlib (yes, my "feeloutXrange" there is a hack so bad it had to be shown)

Stafani answered 27/1, 2017 at 0:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.