Пример #1
0
def dot(u, v):
    """
    Return a dot product output between matrices and vectors.
    Specially, Multiplication between matrix and vector is represented by
    linear combination of column vectors of matrix that scalar multiplied
    by coressponding element of vector, v[c].
    """
    if type(u) is gvec.vector and type(v) is gvec.vector:
        if u.domain == v.domain:
            return sum([u.getItem(d) * v.getItem(d) for d in u.domain])
    elif type(u) is gmat.matrix and type(v) is gvec.vector:
        (R, C) = u.domain
        if C == v.domain:
            output = gvec.vector(R, {})
            for c in C:
                output = output + u.col(c) * v.getItem(c)
            return output
    elif type(u) is gmat.matrix and type(v) is gmat.matrix:
        (Ru, Cu) = u.domain
        (Rv, Cv) = v.domain
        if Cu == Rv:
            output = gmat.matrix((Ru, Cv), {})
            for cv in Cv:
                column_vec = dot(u, v.col(cv))
                output.addColumn(cv, column_vec)
            return output
    return None
Пример #2
0
 def col(self, c):
     """
     Return a column vector.
     Parameter 'c' is element of column set.
     """
     (row, col) = self.domain
     if c not in col:
         return None
     return gvec.vector(
         row, {r: self.func[(r, c)]
               for r in row if (r, c) in self.func})
Пример #3
0
 def row(self, r):
     """
     Return a row vector.
     Parameter 'r' is element of column set.
     """
     (row, col) = self.domain
     if r not in row:
         return None
     return gvec.vector(
         col, {c: self.func[(r, c)]
               for c in col if (r, c) in self.func})
Пример #4
0
import gula.mat as gmat
import gula.util as gutil
"""
R is row label of matrix.
C is column label of matrix.
"""
R = {'a', 'b'}
C = {'#', '@', '?'}

A = gmat.matrix((R, C),
                {d: i + 1
                 for i, d in enumerate(gutil.cartesianProduct(R, C))})
print('Matrix A=')
print(A)

x = gvec.vector(C, {c: 1 for c in C})
print('Vector x=')
print(x)
"""
Set(Label) C is a domain of function.
Set R is a co-domain of function.
So, RxC matrix is model of (C->R) function.
"""

print('Dot product, A*x=b')
print('Vector b=')
print(gutil.dot(A, x))
"""
(x->M*x) is function of (C->R).
The domain of vector b is range of above function.
"""
Пример #5
0
# Author  : Gu-hwan Bae
# Date    : Sun Jan 27
# Summary : Test a vector model and its operations.

import gula.vec as gvec

D = {'A', 'B', 'C'}
func = {'A': 1, 'B': 2}
v = gvec.vector(D, func)
print('Vector ,v')
print(v)

# Make a zero vector.
z = gvec.zeros(D)
print('Zero vector, z')
print(z)

# Add a mapping expression to vector, v.
print('Set a mapping, C - 3, to the vector')
v.setItem('C', 3)
print(v)

# Doubling a vector, v.
v_double = v * 2  # Elementwise multiplication.
print('Doubled vector, v_double')
print(v_double)

# Make a new vector, u, that has same domain as v.
u = gvec.vector(v.domain, {'A': 5, 'C': 10})
print('Vector, u, with same domain as v.')
print(u)
Пример #6
0
print('Identical matrix, I, 3x3')
print(I)

print('Row vectors of Matrix M')
(row, col) = M.domain
for r in row:
    print('Row vector, r =', r)
    print(M.row(r))
for c in col:
    print('Column vector, c =', c)
    print(M.col(c))

A = gmat.matrix(({'x', 'y'}, {'a', 'b', 'c'}),
                {('x','a'):1.,
                 ('x','b'):2.,
                 ('x','c'):3.,
                 ('y','a'):4.,
                 ('y','b'):5.,
                 ('y','c'):6.})
print('2x3 Matrix, A')
print(A)

(row, col) = A.domain
x = gvec.vector(col, {'a':.1, 'b':.2, 'c':.3})
print('R-3 Vector, x')
print(x)

b = gutil.dot(A, x)
print('Multiplying matrix and vector, Ax = b')
print('Output vector, b')
print(b)