简体   繁体   English

如何制作矢量数学程序?

[英]How to make a vector math program?

so i want to create a vectormath program, that has 3 calculation dimensions.所以我想创建一个矢量数学程序,它有 3 个计算维度。 addition(addition of vectors) dot( the sum of the products) normalization(the norm of a single vector is the square root of the sum of the squares) addition(向量相加) dot(乘积之和) normalization(单个向量的范数是平方和的平方根)

if i have 2 vectors: A = (1,3,2) B = (2,3,0)如果我有 2 个向量:A = (1,3,2) B = (2,3,0)

Addition: A + B = (1 + 2, 3 + 3, 2 + 0) = (3,6,2)加法:A + B = (1 + 2, 3 + 3, 2 + 0) = (3,6,2)

Dot: AB = 1.2 + 3.3 + 2.0 = 2 + 9 + 0 = 11点:AB = 1.2 + 3.3 + 2.0 = 2 + 9 + 0 = 11

Norm (of A): A = Sqrt(1^2 + 3^2 + 2^2) = Sqrt(14) = 3.74范数(A):A = Sqrt(1^2 + 3^2 + 2^2) = Sqrt(14) = 3.74

B = Sqrt(2^2 + 3^2 + 0^2) = Sqrt(4+9+0) = Sqrt(13) = 3.61 B = Sqrt(2^2 + 3^2 + 0^2) = Sqrt(4+9+0) = Sqrt(13) = 3.61

Sample Output:样本 Output:

Enter vector a:

1 3 2

Enter vector b:

2 3 0

A + B = [3, 6, 2]

A.B = 11

A = 3.74

B = 3.61

Thanks in advance.提前致谢。

Here you are:)这个给你:)

from math import sqrt

class Vector():
    def __init__(self, x=0, y=0, z=0):
        self.x = x
        self.y = y
        self.z = z

    def __add__(self, v):
        x = self.x + v.x
        y = self.y + v.y
        z = self.z + v.z
        return Vector(x, y, z)

    def dot(self, v):
        xdot = self.x * v.x
        ydot = self.y * v.y
        zdot = self.z * v.z
        return xdot + ydot + zdot

    def norm(self):
        return sqrt(self.x**2 + self.y**2 + self.z**2)

    def __str__(self):
        return "({}, {}, {})".format(self.x, self.y, self.z)


a = Vector(1, 3, 2)
b = Vector(2, 3, 0)

print("a", a)
print("b", b)
print("a + b : ", a + b)
print("norm of a : {}".format(round(a.norm(), 2)))
print("norm of b : {}".format(round(b.norm(), 2)))

output: output:

a (1, 3, 2)
b (2, 3, 0)
a + b :  (3, 6, 2)
norm of a : 3.74
norm of b : 3.61

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM