gpt4 book ai didi

arrays - 变量矩阵 NumPy 的值

转载 作者:行者123 更新时间:2023-12-02 06:54:32 25 4
gpt4 key购买 nike

我正在开发一个程序来确定直线是否相交。我正在使用矩阵来做到这一点。我了解所有数学概念,但我对 Python 和 NumPy 还不熟悉。

我想将斜率变量和 yint 变量添加到新矩阵中。它们都是花车。我似乎无法弄清楚输入它们的正确格式。这是一个例子:

import numpy as np

x = 2
y = 5
w = 9
z = 12

我知道如果我只是输入原始数字,它看起来会像这样:

matr = np.matrix('2 5; 9 12')

不过,我的目标是输入变量名称而不是整数。

最佳答案

你可以这样做:

M = np.matrix([[x, y], [w, z]])

# or
A = np.array([[x, y], [w, z]])

我也包含了数组,因为我建议使用数组而不是矩阵。虽然矩阵一开始看起来是个好主意(或者至少对我来说是这样),但在我看来,使用数组可以避免很多麻烦。 Here's a comparison of the two that will help you decide which is right for you.

我能想到的数组的唯一缺点是矩阵乘法运算不太漂亮:

# With an array the matrix multiply like this
matrix_product = array.dot(vector)

# With a matrix it look like this
matrix_product = matrix * vector

关于arrays - 变量矩阵 NumPy 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13388826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com