2

Allright. So I try to get solutions to 3 linear equations, using a 3x3 coefficient matrix (named c) and a dependent valuables array (named d). MY CODE:

import numpy as np

import sympy as sym

Ax, Ay, By, M0, F, q, L, L1, L2 = sym.symbols('A_x, A_y, B_y, M_0, F, q, L, L_1, L_2')

p = {'L': 6, 'L_1': 4, 'L_2': 2, 'q': 2000, 'F': 1000, 'M_0': 4000} 

eq_Fx = sym.Eq(Ax - F, 0)

eq_MB = sym.Eq(-Ay*L + q*L1*(L1/2) + M0, 0)

eq_MA = sym.Eq(By*L + M0 - L1*q*(L1/2), 0)

c = np.array([[1, 0, 0], 

[0, p['L'], 0], 

[0, 0, p['L']]])

d = np.array([F, 

(p['q']*p['L_1']**2)/2 + p['M_0'], 

(p['q']*p['L_1']**2)/2 - p['M_0']]) 

result = np.linalg.solve(c, d)

print(result)

It raises this weird error which I do not understand:

TypeError: No loop matching the specified signature and casting was found for ufunc solve1.

I would be beyond thankful if anyone even knows what that means or how I can correct it.

Loïc G.
  • 3,087
  • 3
  • 24
  • 36
Eva B
  • 21
  • 1
  • 1
  • 3
  • Please don't post images of code in SO (read [this link](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) if you wonder why) – Loïc G. Dec 12 '17 at 18:41
  • Okay, sorry. I am solving this system: c = np.array([[1, 0, 0], [0, p['L'], 0], [0, 0, p['L']]]) d = np.array([F, (p['q']*p['L_1']**2)/2 + p['M_0'], (p['q']*p['L_1']**2)/2 - p['M_0']]) Where c is the coefficient matrix and d is my dependant valuables array. Those are the data I am using: p = {'L': 6, 'L_1': 4, 'L_2': 2, 'q': 2000, 'F': 1000, 'M_0': 4000} So I tried solving the system in this way: result = np.linalg.solve(c, d) print(result) But it doesn't work. :/ – Eva B Dec 12 '17 at 18:54
  • Edit your question and post your code into it – Loïc G. Dec 12 '17 at 19:41
  • I did. Thanks. (I'm new to Stack, sorry). – Eva B Dec 12 '17 at 21:20
  • 1
    Possible duplicate of [Error solving Matrix equation with numpy](https://stackoverflow.com/questions/36689080/error-solving-matrix-equation-with-numpy). `F` is of type `` while others elements of the array defined in `d` are of type `float`. Maybe `d = np.array([F, ...]` should be `d = np.array([p['F'], ...]`? – Loïc G. Dec 12 '17 at 22:19
  • OMG! Yes! I overlooked that.. thank you so much, it works now! – Eva B Dec 13 '17 at 16:44

1 Answers1

1

If you want to solve or manipulate the equations symbolically, you should use SymPy. If you want a numeric solution, you should use NumPy. If you want to start with SymPy expressions and go to NumPy functions, you should use lambdify.

In your code, you created some SymPy equations, but you aren't using them. The problem is that you used F instead of p['F'] in your definition of d, creating a NumPy array with an unevaluated SymPy symbol F, which it doesn't know how to deal with. Actually if you remove the SymPy parts entirely your code will still work, since you're not using it at all to derive the equations or anything.

By the way, since your equation is linear, SymPy can solve it exactly. Here's how to do that if you are interested.

>>> p = {M0: 4000, L1: 4, F: 1000, L: 6, L2: 2, q: 2000}
>>> solve([eq_Fx.subs(p), eq_MB.subs(p), eq_MA.subs(p)], [Ax, Ay, By])
{B_y: 2000, A_x: 1000, A_y: 10000/3}

Note that I defined p using the symbols themselves, not strings, so that subs would replace them.


To actually generate the equations with SymPy and convert them to NumPy with lambdify, you might use something like

>>> linear_eq_to_matrix([eq_Fx, eq_MB, eq_MA], [Ax, Ay, By])
(Matrix([
[1,  0, 0],
[0, -L, 0],
[0,  0, L]]), Matrix([
[                F],
[-L_1**2*q/2 - M_0],
[ L_1**2*q/2 - M_0]]))
>>> C, D = linear_eq_to_matrix([eq_Fx, eq_MB, eq_MA], [Ax, Ay, By])
>>> p = {'L': 6, 'L_1': 4, 'L_2': 2, 'q': 2000, 'F': 1000, 'M_0': 4000}
>>> fC = lambdify([M0, F, q, L, L1, L2], C, 'numpy', dummify=False)
>>> fC(**p)
array([[ 1,  0,  0],
       [ 0, -6,  0],
       [ 0,  0,  6]])
>>> c = fC(**p)
>>> fD = lambdify([M0, F, q, L, L1, L2], D, 'numpy', dummify=False)
>>> d = fD(**p)
>>> import numpy
>>> np.linalg.solve(c, d)
array([[ 1000.        ],
       [ 3333.33333333],
       [ 2000.        ]])

Here I'm defining p using strings again, so that we can use them as keyword arguments to the lambdified functions (otherwise you have to pass them in order, like fC(4000, 1000, 2000, 6, 4, 2)). The dummify=False argument is also necessary to make this work.

The linear_eq_to_matrix function converts the symbolic equations into SymPy matrices. lambdify then lets you convert these SymPy matrices with symbolic elements into numeric NumPy arrays.

A final note: you would avoid a lot of confusion for yourself if you were consistent in the use or non-use of underscores in your Symbol and variable names, e.g., either use

M0 = symbols("M0")

or

M_0 = symbols("M_0")

SymPy will print the 0 as a subscript in either case.

asmeurer
  • 86,894
  • 26
  • 169
  • 240