UFuncTypeError: Cannot cast ufunc 'det' input from dtype('O') to dtype('float64') with casting rule 'same_kind'? How to avoid this issue?

UFuncTypeError: Cannot cast ufunc 'det' input from dtype('O') to dtype('float64') with casting rule 'same_kind'? How to avoid this issue?

我正在尝试在 python 中构建 PDE。我对此很陌生,想知道我哪里出错了。会很感激一些帮助。我知道我有一个 python 对象,我正在尝试将其转换为 float64,但是有什么办法可以解决这个问题吗?

这是我的错误

---------------------------------------------------------------------------
UFuncTypeError                            Traceback (most recent call last)
<ipython-input-10-0b4b2c2546dc> in <module>()
     33 I1 = np.trace(C)
     34 
---> 35 J = np.linalg.det(F)
     36 
     37 D = np.linalg.inv(C)

<__array_function__ internals> in det(*args, **kwargs)

/usr/local/lib/python3.7/dist-packages/numpy/linalg/linalg.py in det(a)
   2156     t, result_t = _commonType(a)
   2157     signature = 'D->D' if isComplexType(t) else 'd->d'
-> 2158     r = _umath_linalg.det(a, signature=signature)
   2159     r = r.astype(result_t, copy=False)
   2160     return r

UFuncTypeError: Cannot cast ufunc 'det' input from dtype('O') to dtype('float64') with casting rule 'same_kind'

这是我的代码

import numpy as np
from sympy import Symbol, Function, Number

# coordinates
x, y = Symbol('x'), Symbol('y')
normal_x, normal_y = Symbol('normal_x'), Symbol('normal_y')

# time
t = Symbol('t')

# make input variables
input_variables = {'x': x, 'y': y}
# A 1D array
u = np.array([10, 20, 30])
v = np.array([10, 20, 30])

u = Function('u')(*input_variables)
v = Function('v')(*input_variables)
 
Exx = u.diff(x)
Eyy = v.diff(y)
Exy = 0.5 * (u.diff(x) + v.diff(y))
I = np.identity(2)

grad_u = np.array([[Exx, Exy], [Exy, Eyy]])

F = np.add(grad_u, I)

F_t = np.transpose(F)

C = np.matmul(F_t, F)

I1 = np.trace(C)

J = np.linalg.det(F)

D = np.linalg.inv(C)

con1 = (J**(-2/3))*mu
con2 = (K/2)*(J**2 - 1)

S = con1*(I - np.matmul((I1/3), D)) + con2*D 

像这样的符号计算应该用 sympy 而不是 numpy 来完成。没有充分的理由将 numpy 用于您正在做的任何部分,因此在您了解不同库的用途之前,最好在使用 sympy 时避免完全使用它:

from sympy import Symbol, Function, Number, eye, Matrix, factor

# coordinates
x, y = Symbol('x'), Symbol('y')
normal_x, normal_y = Symbol('normal_x'), Symbol('normal_y')

# time
t = Symbol('t')

# make input variables
input_variables = {'x': x, 'y': y}
# A 1D array
u = [10, 20, 30]
v = [10, 20, 30]

u = Function('u')(*input_variables)
v = Function('v')(*input_variables)
 
Exx = u.diff(x)
Eyy = v.diff(y)
Exy = (u.diff(x) + v.diff(y)) / 2
I = eye(2)

grad_u = Matrix([[Exx, Exy], [Exy, Eyy]])

F = grad_u + I

F_t = F.T

C = F_t @ F

I1 = C.trace()

J = F.det()

D = C.adjugate() / C.det()
D = D.applyfunc(factor)

# I guess you need to define some more symbols here:
#
#con1 = (J**(-S(2)/3))*mu
#con2 = (K/2)*(J**2 - 1)
#
#S = con1*(I - np.matmul((I1/3), D)) + con2*D