Join us on Discord!
You can help CodeWalrus stay online by donating here.
Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Topics - Thecoder1998

#1
hi guys, i made a vectory library in python :)
i'm looking for some more things to implement, maybe you guys could come up with some ideas

from numpy import matrix, arctan, arccos, sqrt
def vec(a, b, c = None, d = None):
    if c == None and d == None:
        return matrix('%s;%s' % (a, b))
    elif c != None and d == None:
        return matrix('%s;%s;%s' % (a, b, c))
    else:
        return matrix('%s;%s;%s;%s' % (a, b, c, d))
def dim(v):
    return v.shape[0]
def comp(v, a):
    return v.item(a - 1, 0)
def dot(v1, v2):
    if dim(v1) == dim(v2):
        dot = 0
        for i in range (dim(v1)):
            dot = dot + comp(v1, i) * comp(v2, i)
        return dot
    else:
        return "error"
def cross(v1, v2):
    if dim(v1) == 3 and dim(v2) == dim(v1):
        u1 = comp(v1, 1)
        u2 = comp(v1, 2)
        u3 = comp(v1, 3)
        w1 = comp(v2, 1)
        w2 = comp(v2, 2)
        w3 = comp(v2, 3)
        n1 = u2 * w3 - u3 * w2
        n2 = u3 * w1 - u1 * w3
        n3 = u1 * w2 - u2 * w1
        return vec(n1, n2, n3)
    else:
        return "error"
def stp(v1, v2, v3):
    if dim(v1) == dim(v2) and dim(v2) == dim(v3):
        return dot(v1, cross(v2, v3))
    else:
        return "error"
def vtp(v1, v2, v3):
    if dim(v1) == dim(v2) and dim(v2) == dim(v3):
        return cross(v1, cross(v2, v3))
    else:
        return "error"
def length(v):
    return sqrt(dot(v, v))
def angle(v1, v2):
    if dim(v1) == dim(v2):
        return arccos(dot(v1, v2) / (length(v1) * length(v2)))
    else:
        return "error"
def printv(v):
    if v != "error":
        if dim(v) == 2:
            a = comp(v, 1)
            b = comp(v, 2)
            vec = '(%s, %s)' % (a, b)
        if dim(v) == 3:
            a = comp(v, 1)
            b = comp(v, 2)
            c = comp(v, 3)
            vec = '(%s, %s, %s)' % (a, b, c)
        if dim(v) == 4:
            a = comp(v, 1)
            b = comp(v, 2)
            c = comp(v, 3)
            d = comp(v, 4)
            vec = '(%s, %s, %s, %s)' % (a, b, c, d)
        print vec
    else:
        print "error"
def printpol(v):
    if v != "error":
        if dim(v) == 2:
            x = comp(v, 1)
            y = comp(v, 2)
            r = length(v)
            theta = arctan(y / x)
            vec = matrix('%s;%s' % (r, theta))
        elif dim(v) == 3:
            x = comp(v, 1)
            y = comp(v, 2)
            z = comp(v, 3)
            r = length(v)
            theta = arctan(y / x)
            phi = arccos(z / r)
            vec = matrix('%s;%s;%s' % (r, theta, phi))
        printv(vec)
    else:
        print "error"

EDIT: Added the .py file so people can be lazy
#2
Hi guys, i'm currently learning how to program in Python and i've made a neat little script which can do arithmetic with quaternions  :)

from numpy import matrix
def quaternion(a,b,c,d):
    u = matrix('1.,0.,0.,0.;0.,1.,0.,0.;0.,0.,1.,0.;0.,0.,0.,1.')
    i = matrix('0.,1.,0.,0.;-1.,0.,0.,0.;0.,0.,0.,-1.;0.,0.,1.,0.')
    j = matrix('0.,0.,1.,0.;0.,0.,0.,1.;-1.,0.,0.,0.;0.,-1.,0.,0.')
    k = matrix('0.,0.,0.,1.;0.,0.,-1.,0.;0.,1.,0.,0.;-1.,0.,0.,0.')
    q = a*u+b*i+c*j+d*k
    return q
def printquaternion(q):
    a = q.item(0,0)
    b = q.item(0,1)
    c = q.item(0,2)
    d = q.item(0,3)
    q = "%s+%si+%sj+%sk"%(a,b,c,d)
    print q

here is a screenshot

#3
So during computer science class we're doing information modeling
And we decided to use codewalrus for our diagram

Pretty cool eh?
Powered by EzPortal