python - Having some trouble with assignment -
i need make functions perform basic algebraic operations , couple of other things on quaternions(these complex numbers of form + xi + yj + z*k). first created class contain attributes, , whenever create instance quaternion. however, when tried implementing functions mentioned before keep getting error messages. anyway, here code in full:
from math import * class quaternion(object): def __init__(self, re, xc, yc, zc): self.a = re self.x = xc self.y = yc self.z = zc def __str__(self): return str(self.a) + "+" + str(self.x) + "i" + "+" + str(self.y) + "j" + "+" + str(self.z) + "k" def add(self, q): self.a = self.a + q.a self.x = self.x + q.x self.y = self.y + q.y self.z = self.z + q.z def mul(self, q): self.a = self.a*q.a - self.x*q.x - self.y*q.y - self.z*q.z self.x = self.a*q.x + self.x*q.a + self.y*q.z - self.z*q.y self.y = self.a*q.y + self.y*q.a + self.z*q.x - self.x*q.z self.z = self.a*q.z + self.z*q.a + self.x*q.y - self.y*q.x def conjugate(self): self.a = self.a self.x = -1 * self.x self.y = -1 * self.y self.z = -1 * self.z def norm(self): return sqrt((self.a)**2+(self.x)**2+(self.y)**2+(self.z)**2) def reciprocal(self): p1 = self.conjugate() self.a = p1.a * (1/(self.norm())**2) self.x = p1.x * (1/(self.norm())**2) self.y = p1.y * (1/(self.norm())**2) self.z = p1.z * (1/(self.norm())**2) def main(): p = quaternion(2, 0, -3, 0) q = quaternion(0, 1, 1, -2) print "p =", p print "q =", q print "p + q =", p.add(q) print "p * q =", p.mul(q) print "conjugate of p is", p.conjugate() print "norm of p is", p.norm() print "reciprocal of p is", p.reciprocal() print "p x reciprocal(p) =", p.mul(p.reciprocal) if __name__ == '__main__': main() now, whenever run module(so executes commands under main function), this:
p = 2+0i+-3j+0k q = 0+1i+1j+-2k p + q = none p * q = none conjugate of p none norm of p 9.11043357914 reciprocal of p the thing right printing out 2 quaternions p , q, none of other functions/methods seem working properly(the norm give value, isnt right 1 reason).
before forget, let me each functions needs do:
add(self, q) needs add 2 quaternions together. mul(self, q) needs multiply 2 quaternions. conjugate(self) needs transform given quaternion + xi + yj + zk form: - xi - yj - zk. norm(self) , reciprocal(self) need respectively return norm , reciprocal of quaternion
you performing math correctly in principle, not returning new object should be.
for example, let's @ add(). when sum 2 objects, expecting return value third object of same type, printing. add() function not return (in python equivalent returning none), , instead unexpectedly modifies object called on. instead, this:
def add(self, q): return quaternion(self.a + q.a, self.x + q.x, self.y + q.y, self.z + q.z) do same other methods. if want use + , * operators in code, change method names __add__ , __mul__. in-place addition , multiplication using += , *= operators, sort of current methods doing, rename current methods __iadd__ , __imul__, don't forget return self @ end.
Comments
Post a Comment