python定义向量内积_Python 设计一个向量类,实现数据的输入、输出、向量的加法、减法、点积、夹角等计算...

Python 设计一个向量类,实现数据的输入、输出、向量的加法、减法、点积、夹角等计算

练习题

2018.10.25

import math

class Vectors:

def __init__(self):

self.x1=0

self.x2=0

self.y1=0

self.y2=0

self.x=self.x2-self.x1

self.y=self.y2-self.y1

def add(self):

self.x1=int(input("input x1 "))

self.y1=int(input("input y1 "))

self.x2=int(input("input x2 "))

self.y2=int(input("input y2 "))

self.x=self.x2-self.x1

self.y=self.y2-self.y1

def out(self):

print(self.x,self.y)

def plus(self,a,b):

self.x=a.x+b.x

self.y=a.y+b.y

def sub(self,a,b):

self.x=a.x-b.x

self.y=a.y-b.y

def pointmuti(self,a,b):

return (a.x*b.x+a.y+b.y)

def angle(self,a,b):

a1=(a.x*a.x+a.y*a.y)**0.5

b1=(b.x*b.x+b.y*b.y)**0.5

ab=a.x*b.x+a.y+b.y

return ab//(a1*b1)

v=Vectors()

a=Vectors()

b=Vectors()

a.add()

b.add()

a.out()

b.out()

v.plus(a,b)

v.out()

v.sub(a,b)

v.out()

print(v.pointmuti(a,b))

print(math.acos(v.angle(a,b)))

你可能感兴趣的:(python定义向量内积)