将矩阵值分配给变量或从矩阵中检索值

发布于 2025-01-16 10:32:43 字数 130 浏览 0 评论 0原文

我试图将由矩阵运算确定的值放入矩阵中,作为单独函数中的值。

本质上:

Matrix=[x,y]

我只需要一种方法来获取 x 和 y 值来使用它们。

函数=xsin(xt)+ycos(yt)

I am trying to put values determined by matrix operations, thus in a matrix, as values in a separate function.

Essentially:

Matrix=[x,y]

I just need a way to get out the x and y values to use them.

Function=xsin(xt)+ycos(yt)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独行侠 2025-01-23 10:32:43

如果您只想要矩阵中的 x 和 y 并将它们分配给可以在方程中使用的变量

Matrix = [10,11] # What is typically referred to as a list

x = Matrix[0]
y = Matrix[1]

print(x)
10
print(y)
11

如果您实际上有一个像 numpy 这样的矩阵,那么可能是以下 问题可能更多你在寻找什么。

If you just want x and y from your matrix and assign them to a variable that you can use in your equation

Matrix = [10,11] # What is typically referred to as a list

x = Matrix[0]
y = Matrix[1]

print(x)
10
print(y)
11

If you actually have a matrix like with numpy then maybe the following question may be more what you are looking for.

三岁铭 2025-01-23 10:32:43

如果您想访问 Matrix 中的 xy

x = Matrix[0]
y = Matrix[1]

如果您想在这些值中进行子项:

equation = "xsin(xt)+ycos(yt)"
Matrix = [2, 4]

equation = equation.replace("x", str(Matrix[0]))
equation = equation.replace("y", str(Matrix[1]))
2sin(2t)+4cos(4t)

如果您尝试在方程,您可能喜欢 sympy

import sympy

equation = "xsin(xt)+ycos(yt)"

# Needs to have * symbols
equation = "x*sin(x*t)+y*cos(y*t)"


def eq(matrix):
    in_dict = {
        "x": matrix[0],
        "y": matrix[1],
        "t": 1  # What is t?
    }

    subs = {sympy.symbols(key): item for key, item in in_dict.items()}
    ans = sympy.simplify(equation).evalf(subs=subs)

    return str(ans)


print(eq([1, 1]))
1.38177329067604

If you want to access the x and y values from Matrix

x = Matrix[0]
y = Matrix[1]

If you want to sub in those values:

equation = "xsin(xt)+ycos(yt)"
Matrix = [2, 4]

equation = equation.replace("x", str(Matrix[0]))
equation = equation.replace("y", str(Matrix[1]))
2sin(2t)+4cos(4t)

If you are trying to sub in the equations, you may like the sympy library

import sympy

equation = "xsin(xt)+ycos(yt)"

# Needs to have * symbols
equation = "x*sin(x*t)+y*cos(y*t)"


def eq(matrix):
    in_dict = {
        "x": matrix[0],
        "y": matrix[1],
        "t": 1  # What is t?
    }

    subs = {sympy.symbols(key): item for key, item in in_dict.items()}
    ans = sympy.simplify(equation).evalf(subs=subs)

    return str(ans)


print(eq([1, 1]))
1.38177329067604
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文