from math import * class vector: def __init__(self, x, y, z): self.x = float(x) self.y = float(y) self.z = float(z) def dot(self, other): return self.x*other.x + self.y*other.y + self.z*other.z def cross(self, other): return vector(self.y*other.z-self.z*other.y, self.z*other.x-self.x*other.z, \ self.x*other.y-self.y*other.x) def mod(self): return pow(self.x**2+self.y**2+self.z**2, 0.5) def __sub__(self, other): return vector(self.x-other.x, self.y-other.y, self.z-other.z) def __add__(self, other): return vector(self.x+other.x, self.y+other.y, self.z+other.z) def __str__(self, precision=2): return str(("%."+"%df"%precision)%float(self.x))+'i'+('+' if self.y>=0 else '-')+ \ str(("%."+"%df"%precision)%float(abs(self.y)))+'j'+('+' if self.z>=0 else '-')+\ str(("%."+"%df"%precision)%float(abs(self.z)))+'k' if __name__ == "__main__": print "Enter x,y,z(separeated by space) value of vector-1" A = vector(*map(float, raw_input().strip().split())) print "Enter x,y,z(separeated by space) value of vector-2" B = vector(*map(float, raw_input().strip().split())) print "A + B: " + str(A+B) print "A - B: " +str(A-B) print "A[.]B: " + str(A.dot(B)) print "A[X]B: " + str(A.cross(B)) print "Modulas of A (|A|): " + str(A.mod()) print "Modulas of B (|B|): "+ str(B.mod()) print "Angle between then in radian is %.2f"%degrees(acos(A.dot(B)/(A.mod()*B.mod())))

**Sample output**:-

>>>

Enter x,y,z(separated by space) value of vector-1

2 4 -5

Enter x,y,z(separated by space) value of vector-2

-1 3 -2

A + B: 1.00i+7.00j-7.00k

A - B: 3.00i+1.00j-3.00k

A[.]B: 20.0

A[X]B: 7.00i+9.00j+10.00k

Modulas of A (|A|): 6.7082039325

Modulas of B (|B|): 3.74165738677

Angle between then in radian is 37.17

**Explanation**:-

Here we have used operator overloading concept to add/subtract vectors. Refer this for another example of operator overloading in Python.In order to find radian value of angle between two vector used math.acos(x) which return the arc cosine of x, in radians.

Very nice post here and thanks for it .I like this blog and really good content.

ReplyDeleteHadoop Admin Training in Chennai

Big Data Administration Training in Chennai

Best IELTS Coaching in Chennai

learn Japanese in Chennai

Best Spoken English Class in Chennai

TOEFL Coaching Centres in Chennai

content writing course in chennai

spanish coaching in chennai

Hadoop Admin Training in Anna Nagar

Hadoop Admin Training in Tnagar

I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts.

ReplyDeletePython Projects for Students

Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

Project Center in Chennai