Author Topic: An Invitation to Explore ... Linear Algebra?  (Read 12168 times)

0 Members and 1 Guest are viewing this topic.

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
coding the matrix: class Vec procedures
« Reply #30 on: February 04, 2016, 08:31:41 pm »
Since there is not much out there on the interwebs as far as hints to build the procedures for this class, I will post the procedures I got to work here.

Essentially this is an exercise in learning the features of Python, since, for the less patient student, there are already libraries like numpy, scipy, and even sage, not to mention the commercial CAS like Mathematica and MATLAB. 

I happened to enjoy doing a little troubleshooting and tinkering to get these to work, and I learned about iterating through the keys and values of dictionary items in the process.

I also got a feel for how arithmetic operators are overloaded using special labels such as __neg__ and __mul__

############################################################################
# Copyright 2013 Philip N. Klein
# implementation code: H

def getitem(v,k):
    """
    Return the value of entry k in v.
    Be sure getitem(v,k) returns 0 if k is not represented in v.f.

    >>> v = Vec({'a','b','c', 'd'},{'a':2,'c':1,'d':3})
    >>> v['d']
    3
    >>> v['b']
    0
    """
    assert k in v.D
    return v.f[k] if k in v.f else 0

def setitem(v,k,val):
    """
    Set the element of v with label d to be val.
    setitem(v,d,val) should set the value for key d even if d
    is not previously represented in v.f, and even if val is 0.

    >>> v = Vec({'a', 'b', 'c'}, {'b':0})
    >>> v['b'] = 5
    >>> v['b']
    5
    >>> v['a'] = 1
    >>> v['a']
    1
    >>> v['a'] = 0
    >>> v['a']
    0
    """
    assert k in v.D
    v.f[k] = val


def equal(u,v):
    """
    Return true iff u is equal to v.
    Because of sparse representation, it is not enough to compare dictionaries

    Consider using brackets notation u[...] and v[...] in your procedure
    to access entries of the input vectors.  This avoids some sparsity bugs.

    >>> Vec({'a', 'b', 'c'}, {'a':0}) == Vec({'a', 'b', 'c'}, {'b':0})
    True
    >>> Vec({'a', 'b', 'c'}, {'a': 0}) == Vec({'a', 'b', 'c'}, {})
    True
    >>> Vec({'a', 'b', 'c'}, {}) == Vec({'a', 'b', 'c'}, {'a': 0})
    True

    Be sure that equal(u, v) checks equalities for all keys from u.f and v.f even if
    some keys in u.f do not exist in v.f (or vice versa)

    >>> Vec({'x','y','z'},{'y':1,'x':2}) == Vec({'x','y','z'},{'y':1,'z':0})
    False
    >>> Vec({'a','b','c'}, {'a':0,'c':1}) == Vec({'a','b','c'}, {'a':0,'c':1,'b':4})
    False
    >>> Vec({'a','b','c'}, {'a':0,'c':1,'b':4}) == Vec({'a','b','c'}, {'a':0,'c':1})
    False

    The keys matter:
    >>> Vec({'a','b'},{'a':1}) == Vec({'a','b'},{'b':1})
    False

    The values matter:
    >>> Vec({'a','b'},{'a':1}) == Vec({'a','b'},{'a':2})
    False
    """
    assert u.D == v.D
    if ((u.f == {}) | (v.f == {})):
        if (u.f == {}):
            u = Vec(u.D, {d:0 for d in u.D})
        if (v.f == {}):
            v = Vec(v.D, {d:0 for d in v.D})

    counter = 0
    for k in u.D:
        if v[k] == u[k]:
            counter += 1
    return counter == len(u.D)

def add(u,v):
    """
    Returns the sum of the two vectors.
   
    Consider using brackets notation u[...] and v[...] in your procedure
    to access entries of the input vectors.  This avoids some sparsity bugs.

    Do not seek to create more sparsity than exists in the two input vectors.
    Doing so will unnecessarily complicate your code and will hurt performance.

    Make sure to add together values for all keys from u.f and v.f even if some keys in u.f do not
    exist in v.f (or vice versa)

    >>> a = Vec({'a','e','i','o','u'}, {'a':0,'e':1,'i':2})
    >>> b = Vec({'a','e','i','o','u'}, {'o':4,'u':7})
    >>> c = Vec({'a','e','i','o','u'}, {'a':0,'e':1,'i':2,'o':4,'u':7})
    >>> a + b == c
    True
    >>> a == Vec({'a','e','i','o','u'}, {'a':0,'e':1,'i':2})
    True
    >>> b == Vec({'a','e','i','o','u'}, {'o':4,'u':7})
    True
    >>> d = Vec({'x','y','z'}, {'x':2,'y':1})
    >>> e = Vec({'x','y','z'}, {'z':4,'y':-1})
    >>> f = Vec({'x','y','z'}, {'x':2,'y':0,'z':4})
    >>> d + e == f
    True
    >>> d == Vec({'x','y','z'}, {'x':2,'y':1})
    True
    >>> e == Vec({'x','y','z'}, {'z':4,'y':-1})
    True
    >>> b + Vec({'a','e','i','o','u'}, {}) == b
    True
    """
    assert u.D == v.D
    return Vec(u.D, {d:getitem(u,d)+getitem(v,d) for d in u.D})

def dot(u,v):
    """
    Returns the dot product of the two vectors.

    Consider using brackets notation u[...] and v[...] in your procedure
    to access entries of the input vectors.  This avoids some sparsity bugs.

    >>> u1 = Vec({'a','b'}, {'a':1, 'b':2})
    >>> u2 = Vec({'a','b'}, {'b':2, 'a':1})
    >>> u1*u2
    5
    >>> u1 == Vec({'a','b'}, {'a':1, 'b':2})
    True
    >>> u2 == Vec({'a','b'}, {'b':2, 'a':1})
    True
    >>> v1 = Vec({'p','q','r','s'}, {'p':2,'s':3,'q':-1,'r':0})
    >>> v2 = Vec({'p','q','r','s'}, {'p':-2,'r':5})
    >>> v1*v2
    -4
    >>> w1 = Vec({'a','b','c'}, {'a':2,'b':3,'c':4})
    >>> w2 = Vec({'a','b','c'}, {'a':12,'b':8,'c':6})
    >>> w1*w2
    72

    The pairwise products should not be collected in a set before summing
    because a set eliminates duplicates
    >>> v1 = Vec({1, 2}, {1 : 3, 2 : 6})
    >>> v2 = Vec({1, 2}, {1 : 2, 2 : 1})
    >>> v1 * v2
    12
    """
    assert u.D == v.D
    return sum(v.f[key]*u.f[key] for key in v.f)
    #s = 0
    #for key in v.f:
    #    s += v.f[key]*u.f[key]
    #return s


def scalar_mul(v, alpha):
    """
    Returns the scalar-vector product alpha times v.

    Consider using brackets notation v[...] in your procedure
    to access entries of the input vector.  This avoids some sparsity bugs.

    >>> zero = Vec({'x','y','z','w'}, {})
    >>> u = Vec({'x','y','z','w'},{'x':1,'y':2,'z':3,'w':4})
    >>> 0*u == zero
    True
    >>> 1*u == u
    True
    >>> 0.5*u == Vec({'x','y','z','w'},{'x':0.5,'y':1,'z':1.5,'w':2})
    True
    >>> u == Vec({'x','y','z','w'},{'x':1,'y':2,'z':3,'w':4})
    True
    """
    return Vec(v.D, {d:alpha*value for d, value in v.f.items()})


def neg(v):
    """
    Returns the negation of a vector.

    Consider using brackets notation v[...] in your procedure
    to access entries of the input vector.  This avoids some sparsity bugs.

    >>> u = Vec({1,3,5,7},{1:1,3:2,5:3,7:4})
    >>> -u
    Vec({1, 3, 5, 7},{1: -1, 3: -2, 5: -3, 7: -4})
    >>> u == Vec({1,3,5,7},{1:1,3:2,5:3,7:4})
    True
    >>> -Vec({'a','b','c'}, {'a':1}) == Vec({'a','b','c'}, {'a':-1})
    True
    """
    return Vec(v.D, {d:-getitem(v, d) for d in v.D})





Note that in order to upload, vec.py was renamed vec.txt

Download and save as vec.py before "from vec import Vec" in Python session.

« Last Edit: February 07, 2016, 03:39:40 pm by H »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Holden

  • { ∅, { ∅ } }
  • Posts: 5070
  • Hentrichian Philosophical Pessimist
Re: An Invitation to Explore ... Linear Algebra?
« Reply #31 on: February 05, 2016, 03:18:28 pm »
Quote
Maybe that theory will help me make sense of why I still have to think so hard when ever I contemplate the field of complex numbers.  I think I'm gaining on it, though.  Maybe before I croak ...
You write these seemingly innocuous things-but they are in fact profound and I end up thinking for days about them:
If the individual is absolutely impermanent ,a kind of illusion,a flash in the pan...so is the race,so is the world and finally  so is the universe,for after all individuals make up the whole.We are to REMEMBER/COMPREHEND what? A vast illusion? An impermanent flash in the pan? A mere congeries of phenomena,transitory,vain,non-substantial,unreal,like myself!!!

Is it not absurd to talk of any kind of knowledge? Can there be any such thing?Nay,but if I am not real,permanent,eternal ,true and absolute and if you are not,how can there be any such thing at all?

Could this be the reason why we have to think so hard whenever you & I contemplate the field of complex number?
Could it be that what we are contemplating is an illusion,its just not there-never was.

« Last Edit: February 05, 2016, 03:30:34 pm by Holden »
La Tristesse Durera Toujours                                  (The Sadness Lasts Forever ...)
-van Gogh.

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
An Invitation to Explore ... madness via mathematics?
« Reply #32 on: February 05, 2016, 06:23:24 pm »
You would have liked the last couple of pages of Sarah Perry's Every Cradle is a Grave.   Even our very idea of personal identity and the story of our life is just a machine for making us concerned about our organism.  But stories are not real.  They are constructs that we apply to the universe, but there is no story out in the universe.  There is no "point" to the universe. 

I have spent my life thinking, and supposedly I think too much, and in the end, what difference has it made?

And yet this may be the very shift in the mind Schopenhauer had glorified, to rub the sleep out of our eyes and wonder what the hell is going on here ... all some kind of illusion.

Maybe our interest in mathematics is more akin to madness than to science. 

Quote from: Edgar Allen Poe
Men have called me mad but the question is not yet settled, whether madness is or is not the loftiest intelligence - whether much that is glorious - whether all that is profound - does not spring from the disease of thought - from modes of mind exhaulted at the expense of the general intellect.

I am experiencing subtle insights that I do appreciate, like visualizing the unit circle in my mind, and as the angle increases, seeing the cosine shrink as the sine increases.  I know I read something along these lines by Schopenhauer long ago, and now it is happening in my own mind, and I just treasure this inner world that is a million miles away from "superbowls" and the stock market, the price of beer and concerns about not being able to "please a woman."

Before I forget, I found a link to the Poor Man's Linear Algebra Textbook.   I don't really want to go insane thinking about these ideas.   It actually is more important to me to keep going over the fundamentals.   So, much in that link does not interest me.

I did find Gilbert Strang's videos online.  I used to watch his lectures to supplement the text during the university days.  I am so much more content approaching this subject without the "guidance" of professors, although I appreciate their instruction.  I don't know what it is, but I don't think I ever want to return to the university.  It's all just so much make-believe.

And yet, here is a cool talk by someone in front a chalk board who goes through a very elementary calculation, and yet, the reason I like it is because it includes a couple trigonometric identities that students are told to "memorize".

No, and again no, it is much more beneficial to derive the identities through calculations so as to be able to reproduce the formulas from scratch. 

Rotation Matrices - Or Why You Should Love Trigonometry

« Last Edit: February 07, 2016, 09:04:33 am by H »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
Exploring Linear Algebra: Are we having fun yet?
« Reply #33 on: February 05, 2016, 08:59:49 pm »
Here is someone I will refer to as Kid Kalid.  He is very clear.  In fact, I remember reading his take on the complex field, and I will not play down the impact this had on my intuitive feel for complex numbers.

Anyway, here's his take on Linear Algebra.

An Intuitive Guide to Linear Algebra by Kalid Azad

Of course, there is still Cartwright's Schopenhauer biography.  I haven't forgotten about that.  Maybe after I am released from the day jail "treatment" center, I will be more likely to put an hour or so per day aside to remember what a great influence that man's life has had on my own approach to getting through a life not worth living.

By the way, here are the lectures for Coding the Matrix.  There is also a direct list of recorded lectures.

I'm more than a little frustrated with the code in the book.  The "pretty printing" doesn't work.  I get the following error:

TypeError: non-empty format string passed to object.__format__

- and the printing procedure in class Mat is a bit too cryptic for me to debug.  I have to manage my anger.  Is there no compensation for this feeling of total impotency?  I am behind the eight ball again surfing the learning curve of Sisyphus. >:(

I'm not even sure if printing the Matrix object is worth the trouble of tracking down this nasty little bug, but it becomes an obsession.  I say it's a diabolic bug because I did not write this code.  The author expects us to build the procedures ourselves, but we are advised not to touch the class definition, but that it the code that is generating this little error.

Disregard the following quote.   It's just the part where the TypeError is coming from, the line starting with s4 =

Quote
    def __str__(M, rows=None, cols=None):
        "string representation for print()"
        if rows == None: rows = sorted(M.D[0], key=repr)
        if cols == None: cols = sorted(M.D[1], key=repr)
        separator = ' | '
        numdec = 3
        pre = 1+max([len(str(r)) for r in rows])
        colw = {col:(1+max([len(str(col))] + [len('{0:.{1}G}'.format(M[row,col],numdec)) if isinstance(M[row,col], int) or isinstance(M[row,col], float) else len(str(M[row,col])) for row in rows])) for col in cols}
        s1 = ' '*(1+ pre + len(separator))
        s2 = ''.join(['{0:>{1}}'.format(str(c),colw[c]) for c in cols])
        s3 = ' '*(pre+len(separator)) + '-'*(sum(list(colw.values())) + 1)
        s4 = ''.join(['{0:>{1}} {2}'.format(str(r), pre,separator)+''.join(['{0:>{1}.{2}G}'.format(M[r,c],colw[c],numdec) if isinstance(M[r,c], int) or isinstance(M[r,c], float) else '{0:>{1}}'.format(M[r,c], colw[c]) for c in cols])+'\n' for r in rows])
        return '\n' + s1 + s2 + '\n' + s3 + '\n' + s4

    def pp(self, rows, cols):
        print(self.__str__(rows, cols))

I will forge ahead and take it slow ...  :-[

Nothing is easy.  Everything requires some kind of mind shift.  I guess it's a good kind of mind shift, but I fear Cioran is correct in the end:  life is easier in the horizontal position.  Meaning, I think I'll lay down the cot to see if I can nap.   ???
« Last Edit: February 07, 2016, 05:53:15 pm by H »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
coding the matrix: "pretty printing" buggy in Ipython
« Reply #34 on: February 06, 2016, 05:22:38 pm »
Actually, ipython may be the problem.  Perhaps this code conflicts with ipython's built-in pretty print functions.

In idle3, it worked fine.  Out of curiosity (I was going to launch the debugger), I typed up a couple lines just to see where the bug was in printing the matrix:

from mat import Mat

A = Mat( ({'a','b'}, {'x','y'}), {('a', 'x'):1, ('a', 'y'):2, ('b', 'x'):3, ('b', 'y'):4})
print(A)

the output:

       x y
     -----
 a  |  1 2
 b  |  3 4


So, I will leave that alone and move onto actually building the procedures for vector*matrix, matrix*vector, and matrix*matrix multiplication.
« Last Edit: February 07, 2016, 09:02:39 am by H »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
Coding the Matrix: matrix*vector solution
« Reply #35 on: February 06, 2016, 09:31:57 pm »
With the capacity to print computations in the Python shell (in IDLE3), I was able to tweak my comprehension at the command line to see what was what through trial and error [TINKERING].

I actually jumped for joy when I was able to get the resulting vector for matrix*vector multiplication.   I think it is worth the effort as I may experience more confidence using these "home grown" class objects and their procedures rather than depending solely on modules from NumPy and SciPy. 

Before I go outdoors for a tobacco ceremony break, I wanted to post this little snippet as an example of the tiniest of breakthroughs that may make this "hobby" a worthwhile pursuit while getting through a life not worth living.

def matrix_vector_mul(M, v):
    assert M.D[1] == v.D
    return Vec(M.D[0], {i:sum(M.f[i,j]*v.f[j] for j in v.f) for (i,j) in M.f})

________________________________________________________________
test file:

from mat import Mat
from vec import Vec

A = Mat( ({'a','b', 'c'}, {'x','y'}), {('a', 'x'):1, ('a', 'y'):2, ('b', 'x'):3, ('b', 'y'):4, ('c', 'x'):10, ('c', 'y'):0})
print(A)
b = Vec(A.D[1], {'x':3, 'y':-1})
print(b)
v = A*b
print(v)


        x y
     ------
 a  |   1 2
 b  |   3 4
 c  |  10 0


 x  y
-----
 3 -1

 a b  c
-------
 1 5 30

 ;D
« Last Edit: February 07, 2016, 03:21:29 pm by H »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
Coding the Matrix: vector*matrix solution
« Reply #36 on: February 06, 2016, 11:20:06 pm »
Notice that to construct vector*matrix multiplication, I simply changed the domain from the row set to the column set, so that the output resultant vector would have the same number of columns. I will not go into a detailed explanation, since those details I scribble in my notes.  These notes are here for posterity, should myself or anyone else want a few hints as they make their way through Coding the Matrix.   Myself, I am allowing myself to put some effort into this project as I think it will be more fun to go through the Poole text, especially when we get into Eigenvalues and Eigenvectors in chapter 4, if I have some tools I made myself, with Klein's guidance of course.  I will still use SageMath, NumPy and SciPy as well, but, while I had been somewhat disappointed that all the procedures were not provided, I guess I am gaining more confidence having to stretch my mind a bit in the process.    :-\


def vector_matrix_mul(v, M):
    assert M.D[0] == v.D
    return Vec(M.D[1], {q:sum(M.f[p,q]*v.f[p] for p in v.f) for (p, q) in M.f})

Python shell [test]:

>>> from mat import Mat
>>> from vec import Vec
>>> from imp import reload
>>> c = Vec({'a', 'b'}, {'a':3, 'b':4})
>>> B = Mat(({'a', 'b'}, {'x', 'y', 'z'}), {('a', 'x'): 1, ('a', 'y'): 2, ('a', 'z'): 3, ('b', 'x'): 10, ('b', 'y'):20, ('b', 'z'):30})
>>> print(c)

 a b
----
 3 4
>>> print(B)

        x  y  z
     ----------
 a  |   1  2  3
 b  |  10 20 30

>>> c*B
Vec({'x', 'z', 'y'},{'y': 86, 'z': 129, 'x': 43})
>>> w = c*B
>>> print(w)

  x  y   z
----------
 43 86 129
« Last Edit: February 06, 2016, 11:21:45 pm by H »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
coding the matrix: matrix*matrix solution
« Reply #37 on: February 07, 2016, 12:37:27 pm »
The way I went about coding this procedure is that I started with standard for loops and then, once I got that right, I translated the for loops into Nested Dictionary Comprehension.

As in:


A = Mat(({'x', 'y', 'z'}, {'a', 'b'}), {('x', 'a'): 5, ('x', 'b'): 10, ('y', 'a'): 3, ('y', 'b'): 2, ('z', 'a'): 1, ('z', 'b'):-1})
print(A)
       a  b
     ------
 x  |  5 10
 y  |  3  2
 z  |  1 -1

B = Mat(({'a', 'b'}, {'p', 'q', 'r'}), {('a', 'p'): 1, ('a', 'q'): 2, ('a', 'r'): 3, ('b', 'p'): 10, ('b', 'q'):20, ('b', 'r'):30})
print(B)

        p  q  r
     ----------
 a  |   1  2  3
 b  |  10 20 30

>>> C = Mat((A.D[0], B.D[1]), {})
>>> print(C)

       p q r
     -------
 x  |  0 0 0
 y  |  0 0 0
 z  |  0 0 0

for i in A.D[0]:
     for j in B.D[1]:
        for k in B.D[0]:
           C[i, j] += A.f[i, k] * B.f[k, j]

         
>>> print(C)

         p   q   r
     -------------
 x  |  105 210 315
 y  |   23  46  69
 z  |   -9 -18 -27

Then with nested dictionary comprehension:

D = Mat((A.D[0], B.D[1]), {(i,j):sum(A.f[i, k]*B.f[k, j] for k in B.D[0]) for j in B.D[1] for i in A.D[0]})

>>> D = A*B
>>> print(D)

         p   q   r
     -------------
 x  |  105 210 315
 y  |   23  46  69
 z  |   -9 -18 -27

>>> C == D
Traceback (most recent call last):
  File "<pyshell#296>", line 1, in <module>
    C == D
  File "/home/hentrich/py/linalg/mat.py", line 45, in equal
    if A[k] == B[k]:
  File "/home/hentrich/py/linalg/mat.py", line 15, in getitem
    assert k[0] in M.D[0] and k[1] in M.D[1]
TypeError: 'set' object does not support indexing

So, even though I know C == D, I still have work to do in the definition of procedure equal(A, B) or maybe in the getitem(M, k) procedure. 

Take a bath Max!

All in all, this is a great breakthrough.

EDIT:

I just had to straighten out the equal(A, B) procedure as I suspected.


def equal(A, B):
    assert A.D == B.D
    counter = 0
    for i in A.D[0]:
        for j in A.D[1]:
            if (A.f[i, j] == B.f[i, j]):
                counter += 1
    return (counter == len(A.D[0]) * len(A.D[1]))

_______________________________________________________

Then running the test:

from mat import Mat
from vec import Vec

A = Mat(({'x', 'y', 'z'}, {'a', 'b'}), {('x', 'a'): 5, ('x', 'b'): 10, ('y', 'a'): 3, ('y', 'b'): 2, ('z', 'a'): 1, ('z', 'b'):-1})
print("Matrix A: ")
print(A)
B = Mat(({'a', 'b'}, {'p', 'q', 'r'}), {('a', 'p'): 1, ('a', 'q'): 2, ('a', 'r'): 3, ('b', 'p'): 10, ('b', 'q'):20, ('b', 'r'):30})
print("Matrix B: ")
print(B)

print("Using Nested Dictionary Comprehension : A * B = C")
C = A*B
print(C)


print("Using NESTED FOR LOOPS: A * B = D")
D = Mat((A.D[0], B.D[1]), {})
print("Initialize D to zero matrix: ")
print(D)

for i in A.D[0]:
   for j in B.D[1]:
      for k in B.D[0]:
         D[i, j] += A.f[i, k] * B.f[k, j]

print("After computations, Matrix D: ")
print(D)

print("C == D ?")

print(C == D)


___________________________________________________

YIELDS:


>>>
================= RESTART: /home/hentrich/py/linalg/test2.py =================
Matrix A:

       a  b
     ------
 x  |  5 10
 y  |  3  2
 z  |  1 -1

Matrix B:

        p  q  r
     ----------
 a  |   1  2  3
 b  |  10 20 30

Using Nested Dictionary Comprehension : A * B = C

         p   q   r
     -------------
 x  |  105 210 315
 y  |   23  46  69
 z  |   -9 -18 -27

Using NESTED FOR LOOPS: A * B = D
Initialize D to zero matrix:

       p q r
     -------
 x  |  0 0 0
 y  |  0 0 0
 z  |  0 0 0

After computations, Matrix D:

         p   q   r
     -------------
 x  |  105 210 315
 y  |   23  46  69
 z  |   -9 -18 -27

C == D ?
True
>>>

 :)

By George, I think my mind is shifting ...  ;D

Note that while this code is working, when I run "python -m doctest mat.py" 8 items had failures.  When it passes all the doctests, I will replace the file and make a note that all tests passed.

ALL DOCTESTS PASSED ... latest upload as of 2016.02.02 21:25 passed all tests for "python -m doctest mat.py"

I have not created tests for the GF(2) field yet.

As is the protocol here, in order to upload, mat.py was renamed mat.txt

Download and save as mat.py before "from mat import Mat" in Python session.
« Last Edit: February 07, 2016, 09:24:54 pm by H »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Holden

  • { ∅, { ∅ } }
  • Posts: 5070
  • Hentrichian Philosophical Pessimist
Re: An Invitation to Explore ... Linear Algebra?
« Reply #38 on: February 07, 2016, 09:21:20 pm »
I'm sorry I could not write for 2 days due to internet problem.I think what you are doing with mathematics is,you are using it as a metaphor for a reality that cannot be rendered in literal terms.Every quest concludes in silence and mathematics also comes to stop,if by another route.
As George Santayana has written,"a really naked spirit cannot assume that the world is thoroughly intelligible . There may be surds,there may be hard facts,there may be dark abysses before which intelligence must be silent for the fear of going mad.
La Tristesse Durera Toujours                                  (The Sadness Lasts Forever ...)
-van Gogh.

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #39 on: February 07, 2016, 09:33:41 pm »
Yes, you may be right.  I also enjoy the utterly private nature of one's own understanding.  It's like having a treasure that the world cannot even see ... I like that one is forced to be silent when confronted with certain situations. 

As I was engrossed (and even excited) about making little changes to the code as my understanding deepened, much of gort society in the Divided Snakes of Amerika are glued to TV sets going ga-ga over commercials, celebrities, and the super duper doper ball.   ::)

I treasure this inner realm even if it leads into antisocial madness.  It sure beats acting like what the gorts are hyped up about matters in the least.

The satisfaction I experience from stretching my mind a bit, even though I am sometimes troubled by self-doubt and confusion, is an intangible phenomenon that I do treasure.  It encourages me to continue to ignore gort society.

_____________________________________________________________________________________

Here are the final procedures in mat.py - minus the class which I posted above somewhere and did not alter in any way.

Peace, my friend.
__________________________________________________________________________________-

from vec import Vec

def getitem(M, k):
    assert k[0] in M.D[0] and k[1] in M.D[1]
    return M.f[k] if k in M.f else 0


def equal(A, B):
    assert A.D == B.D
    counter = 0
    for i in A.D[0]:
        for j in A.D[1]:
            if (A[i, j] == B[i, j]):
                counter += 1
    return (counter == len(A.D[0]) * len(A.D[1]))


def setitem(M, k, val):
    assert k[0] in M.D[0] and k[1] in M.D[1]
    M.f[k] = val

def add(A, B):
    assert A.D == B.D
    return Mat((A.D[0], B.D[1]), {(i,j):A[i, j]+B[i, j] for j in B.D[1] for i in A.D[0]})

def scalar_mul(M, x):
    return Mat(M.D, {d:x*value for d, value in M.f.items()})

def transpose(M):
     return Mat((M.D[1], M.D[0]), {(q,p):v for (p,q), v in M.f.items()})

def vector_matrix_mul(v, M):
    assert M.D[0] == v.D
    return Vec(M.D[1], {c:sum(M[r,c]*v[r] for r in v.f) for (r,c) in M.f})

def matrix_vector_mul(M, v):
    assert M.D[1] == v.D
    return Vec(M.D[0], {i:sum(M[i,j]*v.f[j] for j in v.f) for (i,j) in M.f})


def matrix_matrix_mul(A, B):
     assert A.D[1] == B.D[0]
   
    return Mat((A.D[0], B.D[1]), {(i,j):sum(A[i, k]*B[k, j] for k in B.D[0]) for j in B.D[1] for i in A.D[0]})
_________________________________________________________________________________

I was using the notation A.f[i, j] but, except for the getitem and setitem procedures, which are only used by the class Mat itself, I use this notation A[i, j], where i and j can actually be symbols from the row label set and the column label set, respectively, such as A['x', 'b'].

This is fairly impressive to me, especially when you consider the C code I was referring to for LU Factorization, where the matrix entries are stored in contiguous memory in column major order.   Using the Python dictionary data structure to represent vectors and matrices is kind of exotic to me.  It stretches my imagination.   :)
« Last Edit: February 07, 2016, 10:29:58 pm by H »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
My hatred for the world of schools and jobs ...
« Reply #40 on: February 19, 2016, 08:13:22 am »
I may leave Coding the Matrix on the shelf for a stretch, leaving off at the chapter on The Basis as I have lost interest in the kinds of projects the author focuses on.  I will get back to it, but other texts have my interest.

I will continue to work my way through the Poole text as I also have the solution manual ($15 used).

I'm going to be boning up on some "Calculus" incorporating SageMath and Python programming into the learning experience.   As I may have mentioned, my intention is to return to Multivariable Calculus, but, this time, with a firmer grasp of Linear Algebra as I want this "Exploration of Linear Algebra" to lead through multivariate mathematics, which will eventually include a hard serious look at differential equations.

I know this is the kind of mathematics "engineering students" are obsessed about in their undergraduate "careers", but I am not concerned about all the academics.  Eventually I would like to just explore "computational physics" and understand it.

I have broken out of my harness, and even though there is supposedly no "caste system" in the Divided Snakes of Amerika, a member of the working class who drops out of the work-force due to "behavioral problems" and then alternates between years of study and years of binge drinking is pretty much the modern day thought-criminal who will find no "place" in society other than "useless deadbeat intellectual" ...  :D

 So, this thread may branch off as there are many overlapping areas ...

If I should stop mentioning these things, it may have to do with hard facts and dark abysses before which intelligence must be silent for the fear of going mad.   My intellect is exhausting itself in order to demonstrate its own limitations.  I have come to hate and despise not only the corporate culture, but also the academic world of grades, competition, and posturing. 

I don't know which motivates me more, love of mathematics and programming, or hatred for the world of schools and jobs.
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Holden

  • { ∅, { ∅ } }
  • Posts: 5070
  • Hentrichian Philosophical Pessimist
Satanic Verses:Dreams in the Witch House
« Reply #41 on: February 19, 2016, 08:50:58 am »
(By Satan here ,I mean Miltonian Satan)

You say you no longer speak like you did in the H.Files.That you are no longer possessed.But could it be that the only difference is that now you are possessed by a demon of a higher order?  (Maybe the earlier demon drove you to speak in the recorder & this one drives you to do math?)

“The Czech mathematician Vopěnka (1993) proposed as an explanation, why Gauss did not publish his results about non-Euclidean geometry: It was a secret and Gauss as a member of the Temple of Satan was not allowed to explain it to nonmembers. The same explanation holds why he did not explain his normal distribution by geometry of the multidimensional space. Mathematicians form a secret society, which does not reveal its secrets.”


And there there is your consumption of large amount of Coffee.It had received a cool reception when it made it to Europe in the 17th century. It was considered to be “Satan’s drink”.( Like you I am trying to drink a lot of coffee).

In Hungary, many mathematicians drink strong coffee, in fact Rényi once said 'a mathematician is a machine which turns coffee into theorems.' At the mathematical institute they make particularly good coffee and when Pósa was not quite 14 I offered him a little strong coffee which he drank with an infinite amount of sugar. My mother was very angry that I gave the little boy strong coffee. I answered that Pósa could have said 'madam, I do a mathematician's work and drink a mathematician's drink. - Paul Erdös

I know that your are obsessed with  abstruse, arcane, oracular math symbols.Could it be that the mathematical equation and the code you put here are actually “SATANIC VERSES”?SATAN the Ultimate Dissident?





PS:Do you think the bad effects of cigarettes is Gort propaganda?I don't smoke.But I know that Gary smokes a lot & you smoke a lot too-does it make your mind better at math?When you are "high" on coffee,can you concentrate on math better then?

[attachment deleted by admin]
« Last Edit: February 19, 2016, 09:10:31 am by Holden »
La Tristesse Durera Toujours                                  (The Sadness Lasts Forever ...)
-van Gogh.

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
In 1994 I got a kick out of the following Integral.

It is rather silly but is a great way to demystify what the dark priests of mathematics call "The Integral Calculus" ... protecting their craft with mystification.

You are correct about my attraction to mathematical notation.  Unfortunately I can't draw the Integral symbol here.  In fact, this may be one of the reasons I prefer writing in notebooks ... the keyboard is too restrictive.

OK, here's a treat.  Like I said, it is very simple, but when written by hand has a weird effect.

In words:

Two times the integral of x squared on the interval [1, 10] with respect to x.

The integral of x^2 is  x^3 divided by 3.

10^3 / 3 = 1000 / 3

(1000/3) - (1/3) = 999/3 = 333

And, of course, outside the integration symbol is 2 ...

It did give me a chuckle back then ... I imagined one day writing it as graffiti ...  :)

It would make for a nice album cover for a death metal band ... not necessarily very welcome on "Amerikan Idol" or other soul-killing contests.

Now that you mention it, it is time for a strong cup of coffee ... I hear even old Schopenhauer enjoyed a very strong cup of that exotic bean. 

Yes, I especially enjoy tobacco now that I only spend less than $20 per month on a one pound sack of "pipe tobacco" that I twist up into cigarettes.   I no longer chain smoke since I am not permitted to smoke indoors ... and this has only increased my love of tobacco.  I mean, it's almost ceremonial at this point.

Christopher Marlowe wanted to invent a religion around tobacco.

The first "European" who brought back tobacco from the "Americas" was put in prison for 7 years as the smoke flowing from the body appeared "Satanic" ...  :D

Yes, we must joke more frequently about such matters.  Seeing my love of studying mathematics and programming for no practical reason as the result of demonic possession of a higher order is wonderful.  This is as good an explanation as any other. 

"Why are you studying math?  Are you trying to become an engineer or something?  You're nearly 50.  Your life is over you loser!"

"Why, I am possessed by demons of a higher order ..."   8)

« Last Edit: February 19, 2016, 09:43:57 am by H »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Holden

  • { ∅, { ∅ } }
  • Posts: 5070
  • Hentrichian Philosophical Pessimist
Re: An Invitation to Explore ... Linear Algebra?
« Reply #43 on: February 20, 2016, 04:34:42 pm »
Quote
"Why are you studying math?  Are you trying to become an engineer or something?  You're nearly 50.  Your life is over you loser!"


Who the hell knows? You may go down as a mathematician greater than Euler my dear friend.

http://www.claymath.org/millennium-problems
Millennium Problems

Yang–Mills and Mass Gap
Experiment and computer simulations suggest the existence of a "mass gap" in the solution to the quantum versions of the Yang-Mills equations. But no proof of this property is known.

Riemann Hypothesis
The prime number theorem determines the average distribution of the primes. The Riemann hypothesis tells us about the deviation from the average. Formulated in Riemann's 1859 paper, it asserts that all the 'non-obvious' zeros of the zeta function are complex numbers with real part 1/2.

P vs NP Problem
If it is easy to check that a solution to a problem is correct, is it also easy to solve the problem? This is the essence of the P vs NP question. Typical of the NP problems is that of the Hamiltonian Path Problem: given N cities to visit, how can one do this without visiting a city twice? If you give me a solution, I can easily check that it is correct. But I cannot so easily find a solution.

Navier–Stokes Equation
This is the equation which governs the flow of fluids such as water and air. However, there is no proof for the most basic questions one can ask: do solutions exist, and are they unique? Why ask for a proof? Because a proof gives not only certitude, but also understanding.

Hodge Conjecture
The answer to this conjecture determines how much of the topology of the solution set of a system of algebraic equations can be defined in terms of further algebraic equations. The Hodge conjecture is known in certain special cases, e.g., when the solution set has dimension less than four. But in dimension four it is unknown.

Poincaré Conjecture
In 1904 the French mathematician Henri Poincaré asked if the three dimensional sphere is characterized as the unique simply connected three manifold. This question, the Poincaré conjecture, was a special case of Thurston's geometrization conjecture. Perelman's proof tells us that every three manifold is built from a set of standard pieces, each with one of eight well-understood geometries.

Birch and Swinnerton-Dyer Conjecture
Supported by much experimental evidence, this conjecture relates the number of points on an elliptic curve mod p to the rank of the group of rational points. Elliptic curves, defined by cubic equations in two variables, are fundamental mathematical objects that arise in many areas: Wiles' proof of the Fermat Conjecture, factorization of numbers into primes, and cryptography, to name three.


For my friend Mr.Michael Hentrich(World's Greatest Indian Mathematician):
« Last Edit: February 20, 2016, 04:56:54 pm by Holden »
La Tristesse Durera Toujours                                  (The Sadness Lasts Forever ...)
-van Gogh.

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #44 on: May 01, 2016, 08:07:15 pm »
I am looking at Linear Algebra Decoded ... it's the kind of software I wish I could build myself.  It is only available for Windows, but I may look into it, not only to serve as a kind of template, but, I must confess, to see if it might help me overcome some mental blocks ... The full version is available if one sends Anibal Rodriguez Fuentes $22.95. 

Knowing the kind of work and care that would go into making this teaching device, I will gladly contribute and accept whatever faults it might have.  I am not too critical of such projects.   It could be fun in ways I do not at first expect.  It might motivate me to keep Linear Algebra close even when I get through the current textbook I'm working through.  It may give me a better idea of what to research for implementing certain things in sympy or sage.

There is also the on-line Linear Algebra Toolkit which I think is written entirely in perl.  What will these eggheads think of next? 

If only we did not have to sleep and eat or shiit and decay and rot stinking in the earth ... Until then, I will keep exploring until I go blind or worse.

Maybe we will discover a novel approach to this supposedly unfathomable phenomenon we call mathematics.  What if the whole idea of professional mathematicians is, like the old priesthoods which claimed to be the only door to divinity, prevents the novice and mediocre enthusiast from approaching the realm of mathematics as a self-ordained mathematician?

Of course, with industries such as National Innovations out there, charging $15,000 for their software, I guess it is easy to see why I consider what I'm up to as "playing in the sandbox" like a make-believe mad scientist.

As it turns out, the free Linear Algebra Toolkit which one uses while connected to the Internet from either Linux or Windows seems to have more functionality than the purchased Linear Algebra Decoder, which is kind of limited.  Also, the font is so small, you have to use the Windows system "magnifying glass" to use it.   But, I'm not complaining.  I was curious to see if working with some software might motivate me to stick with it and break through some mental blocks.

I am actually taking some notes directly from the Linear Algebra Toolkit's results while working through exercises.  It's good to recognize the patterns.
« Last Edit: May 01, 2016, 08:58:24 pm by H »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~