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

0 Members and 2 Guests are viewing this topic.

Holden

  • { ∅, { ∅ } }
  • Posts: 5416
  • Hentrichian Philosophical Pessimist
Re: An Invitation to Explore ... Linear Algebra?
« Reply #15 on: January 22, 2016, 02:10:05 pm »
Thanks for the response.This exploratory approach is certainly something I need to think about.
La Tristesse Durera Toujours                                  (The Sadness Lasts Forever ...)
-van Gogh.

There is but one truly serious philosophical problem and that is suicide.-Camus

Mad Dog Mike

  • { }
  • { ∅, { ∅ } }
  • Posts: 5088
  • Life teaches us not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #16 on: January 23, 2016, 10:33:05 am »
As you know, when I uploaded the little routines written in C++, these are just examples of exploratory learning.  When my code runs into problems, such as free(): invalid pointer, this means that I will eventually return to the code to understand such bugs which may be fixed by creating a class which uses a copy constructor.  That will also motivate more learning. 

Note to self:  C++11 Move Semantics: Rvalue Reference

When my C++ rref code fails, I just run ipython.

From there:

from sympy import *
A = Matrix([[1, -1, 0, 0, -1, 0, 0], [0, 1, -1, -1, 0, 0, 0], [1, 0, -1, 0, 0, -1, 0], [0, 0, 0, 1, 1, -1, 0], [0, 0, 0, 0, 1, 2, 10], [0, 2, 0, 2, -1, 0, 0], [0, 0, 1, -2, 0, -2, 0]])

A
Matrix([
[1, -1,  0,  0, -1,  0,  0],
[0,  1, -1, -1,  0,  0,  0],
[1,  0, -1,  0,  0, -1,  0],
[0,  0,  0,  1,  1, -1,  0],
[0,  0,  0,  0,  1,  2, 10],
[0,  2,  0,  2, -1,  0,  0],
[0,  0,  1, -2,  0, -2,  0]])


A.rref()

(Matrix([
 [1, 0, 0, 0, 0, 0,  7],
 [0, 1, 0, 0, 0, 0,  3],
 [0, 0, 1, 0, 0, 0,  4],
 [0, 0, 0, 1, 0, 0, -1],
 [0, 0, 0, 0, 1, 0,  4],
 [0, 0, 0, 0, 0, 1,  3],
 [0, 0, 0, 0, 0, 0,  0]]), [0, 1, 2, 3, 4, 5])

« Last Edit: January 25, 2016, 06:01:51 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: 5416
  • Hentrichian Philosophical Pessimist
Re: An Invitation to Explore ... Linear Algebra?
« Reply #17 on: January 23, 2016, 05:03:11 pm »
When I read that I thought of a God who keeps running into evolutionary dead ends.
La Tristesse Durera Toujours                                  (The Sadness Lasts Forever ...)
-van Gogh.

There is but one truly serious philosophical problem and that is suicide.-Camus

Mad Dog Mike

  • { }
  • { ∅, { ∅ } }
  • Posts: 5088
  • Life teaches us not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #18 on: January 23, 2016, 05:25:52 pm »
It's total chaos.  That's why I am content just to work on the most elementary and fundamental exercises ... of "advanced" mathematics, of course.

One term I am going to hold onto from from dirty drunken bastard years is "retarded genius."

It just sounds so honest, as in, "I am a retarded genius."

It's OK for me to think I am "smart enough" to study whatever the hell that interests me as long as  I am prepared to feel a little brain-dead along the way.  I live for the little breakthroughs.

Fortunately, there are those enthusiasts who so generously share their code out there.  You just have to be particular and know where to tweak it to your liking.

Note to self:  GJacobi.cpp and GSeidel.cpp for ITERATIVE METHODS.  (testing out the local search engine).
« Last Edit: January 23, 2016, 06:06:31 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 ~

ducktape

  • Escapee From The Gort Mind
  • Posts: 8
  • “Why do we fall? So we can learn to pick ourselves
Re: An Invitation to Explore ... Linear Algebra?
« Reply #19 on: January 25, 2016, 09:02:23 am »
Linear Algebra, what a wonderful part of Mathematics.
To be honest, when it was first introduced in the fourth semester of my college, I couldn't quite grasp the great ideas. Maybe that was because I am not a Mathematics Student. Most of the methods seemed strange, and exercises that we needed to do seemed boring. Mostly because it was all just plain Math for me and I wasn't paying attention to the details.
But now in my sixth semester at college, I have to get myself thouroghly familiar as well as master the basic as well as the complex ideas if I hope to continue in the field of Physics.
Maybe I am more mature/wise now, but it all seems so beautiful to me now. Now I can really see the theorems for what they are and not just something to be learned to score well in the exams. Maybe it's my love towards Physics that's made me sincere towards understanding the concepts of Linear Algebra. You see Dirac's formulation of Quantum Mechanics is all about Linear Algebra. And since, this semester we are being taught Quantum Mechanics and some Linear Algebra(more inclined towards Physics Applications), I am now comprehending it in a far better way.

Maybe seeing the applications of Linear Algebra side-by-side with the theoretical Mathematical part is helpful for me.

I can remember how I used to be excellent at Math when I was a kid. But then in High-school due to my negligence and ignorance towards my classes and studies, I managed to get really bad at it.
And I regret it a lot now.

I seem to have managed to get back on the right track, when I was first introduced to the subject called 'Real Analysis'. Half of the semester I was like, "WHAT THE HELL??" and the other half I was like, "OMG! THAT IS AMAZING". Seems like I had just learnt a lesson in logical reasoning. Somethings that I had taken for granted for all my life, now had to be proven using Math and Logic in the most amazing way. Though it was just an introductory course, as I am a Physics Major, but I consider it to be a great feat to have understood it.

Seems like I got a little side-tracked for a moment. Coming back to Linear Algebra, can you, Mr. H, or xhentric, suggest me some good books, not very difficult, though as I am only a Physics Student and not a very bright Mathematician?

When I was first introduced to the course, I bought a book by D. Hecker and Andrilly-Elementary Linear Algebra.
It seems pretty good to me. The explanations are good. Everything is explained step-by-step, and with lots of examples. However, I noticed a small problem. Now that it is being taught again, I noticed that the echelon form of a matrix is pretty informative in itself. But the book always makes us reduce every matrix to the Reduced  Row Echelon Form, to find out the characteristics of any Vector Space, even the same information is given by the echelon form. Can you help me out with that?

Also, my professor is suggesting me to read the book by Schaum's Series(dunno the author). The problem is, I don't like it. It feels way too much exam oriented and not something that is to be read to learn something.
Also, it's approach is a lot more different from the one used in the book by Andrilli. So I don't really understand it.

Another problem that I am facing is about defining a Vector Space. In my book it says that the space has to follow the ten properties(two closure, and the other 8). However, my professor only checks to see if the closure properties are satisfied and nothing else. Is it right?

I mean I know that to prove something to be a subspace, it is enough to prove that it is non-empty and satisfies the closure properties. But does it apply to Vector Spaces as well?

Ahh, I gotta go now. Taking a break from college and going home to my parents tonight, for a week. Plan to work more on my Quantum Mechanics and Statistical Physicswhile I am there.
Thanks,
Ducktape

Mad Dog Mike

  • { }
  • { ∅, { ∅ } }
  • Posts: 5088
  • Life teaches us not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #20 on: January 25, 2016, 10:05:49 am »
Before I forget, I extended the gauss.cpp code and renamed it rref.cpp.


When it is compiled and run, it first finds solutions using Gaussian elimination, and then it puts the matrix in row reduced echelon form so you can compare results with the work you do by hand.

Since the arithmetic involved involves many fractions, you may find something as simple as graph paper with larger sized squares may help calm your nerves while you are computing and calculating.   I found a notebook with 1 cm squares.

It's certainly not crucial, but I lost all my notes from the year 2000, so this time through I want to document my work clearly ... It's a kind of "spiritual exercise" ...   :-\

Also, many physicists as well as engineers and mathematicians become more motivated to explore computer algebra systems and computer programming itself as this becomes such a powerful tool in assisting analysis and calculations.  I myself am trying to motivate myself to become interested in computing again, and I picked up a giant linear algebra text along with the solution manual.   I have been motivated to create little helper code that hammers out the grunt work arithmetic operations, but I also do the work by hand in a slow calm manner.   I appreciate how easily we are prone to make errors during those low-level calculations, like subtracting from fractions a multiple involving fractions.   It's good to have some basic code helping you check your work.

Also, while Python has many libraries, you might consider downloading Sage which is based in Python code.  Even just as a calculator, Sage has proven to be a useful tool.   I like to work with fractions.   Of course there are libraries out there for working with Rational numbers in normalized form (positive denominator co-prime with numerator) in C++ like boost::Rational, but Sage is great to have when you don't feel like reinventing the wheel.

Anyway, books ... let's see.  If you are interested in pure Python, there is Coding the Matrix, but I have not actually seen the text.  I would have purchased it, but I am afraid it would distract me.  I do appreciate Python but I am trying to force myself to use C++.

There is a little ebook available for a few dollars that shows how to do LU factorization with C++ code, but I do not have an ereader yet. 

So the books you use will have to do with where you are at personally.  Do you have access to compilers?   Are you compiling your own code?



When I studied this formally at the state university 15 years ago, we used Gilbert Strang text, but this time I am using a different approach since some things just didn't click for me (even though I performed well on exams).  I am using Poole's text, but I suggest you search for guides that you find help you personally.   

I agree that Schuam's outline books are too much cramming and not enough genuine insight, which is what I am still searching for after all these years.

Quote
Now that it is being taught again, I noticed that the echelon form of a matrix is pretty informative in itself. But the book always makes us reduce every matrix to the Reduced  Row Echelon Form, to find out the characteristics of any Vector Space, even the same information is given by the echelon form. Can you help me out with that?

If you download rref.txt and rename it rref.cpp then compile it, this code will put the matrix in both forms and you can compare results.  You can also do by hand along with the code.  In initial echelon form you mention, one can solve the system with back-substitution, whereas in reduced row echelon form, where the left side columns are a diagonal of 1's and the rest 0's, the solutions are right there in the rightmost column of the augmented matrix.   Sometimes, the entire bottom row is 0's ... which means there is a free variable.  There may be infinitely many solutions.  The rank of the matrix is the number of nonzero rows in it's echelon form.  The number of free variables will be the total number of variables minus the rank.   A system is inconsistent (has no solution) when there is a row of zeros in the echelon form and a nonzero constant in the leftmost column of the augmented matrix.  You can understand intuitively why this is so:  0*(x1) + 0*(x2) = 4 has no solution.  As soon as you notice this in echelon form of the augmented matrix, if you are doing the work by hand, you can stop.  Then there is no need to continue to row reduced echelon form.


I better attach that file before I forget.  I am in the process of getting a better grasp of these fundamental concepts.  I have found that one can get by and even do fairly well at the university level in the mechanics of calculating solutions but still lack deeper insight in fundamental concepts.  I am trying to deepen my understanding.  I will help if I can, but I will also admit when my understanding is a bit fuzzy.

Textbooks can only help our understanding just so much.  It's a lifelong process.  I know how it feels to want to genuinely understand things on a deeper level.  I even try to think about these things while I am sleeping.   :-\

All I can say is try not to become overwhelmed and get a little enjoyment in the process of learning.

Here is a preview of Ivan Savov's No Bull Shiit Guide To Linear Algebra (85 page pdf file).  You can see from the table of contents that, when the actual book is released, it does include an Appendix B: Introduction to Quantum Physics.  Try to enjoy what you are studying in spite of academic pressure to perform well on exams.

If I were in your shoes, I would invest in Ivan Savov's Guide to Math and Physics

Savov has a lot of knowledge about SymPy, a Python module developed by a bunch of math addicts and physics enthusiasts. 

The better your intuitive grasp of what you are studying, the more comfortable you will be with the mathematical operations.

-----------------------------------------------------------------------------------------------------------------------------

EDIT: Recreated RREF using my Fraction class (see 2018.04.26)
« Last Edit: April 26, 2018, 12:53:15 am by Non Serviam »
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 ~

ducktape

  • Escapee From The Gort Mind
  • Posts: 8
  • “Why do we fall? So we can learn to pick ourselves
Re: An Invitation to Explore ... Linear Algebra?
« Reply #21 on: January 26, 2016, 12:15:12 pm »
Firstly, thanks for the detailed answer.

Secondly, I gotta congratulate you for finding a way to circumvent the freeboards profanity filter.("No Bull Shiit") :P

I guess I will go through the 85 page preview before I buy it. Have added it on my list.

I haven't really used Sage uptil now(am downling it as I write).
I have been teaching myself Scilab though. I also make video-tutorials after I learn something new.
Scilab is great tool to have by my side while working on Linear Algebra or even Numerical Methods.

Also thanks for sharing that rref code.
Last semester I had made a few C++ programs on Linear Algebra like Gauss Elimination, GSeidel, Eigen-values & Eigenvectors.
I guess this time I'll try to make various codes on topics of Linear Algebra using Scilab. As matrices and vectors are more natural to scilab.
So i won't have to write a lot of code for performing simple operations of matrix.

Also I want to thank you for sharing that link to codingthematrix.com.
I am loving the things that we can do with Computer Programming and a knoweledge of Linear Algebra. Things that I had no idea about. I particularly want to work on the face detection thing. But in a better way as in, I want the computer to be able to just look at a photo and identify the objects in it.
Will have to work on my skills significantly but seems like it will be worth it.
I think I'll buy the cheaper kindle version. After verifying if I can read kindle books through my windows phone or not.

PS: why the heck is Sage so big(2.2gigs)!!?? It better be worth it.
Will keep you posted on my progress through the topics.




Mad Dog Mike

  • { }
  • { ∅, { ∅ } }
  • Posts: 5088
  • Life teaches us not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #22 on: January 26, 2016, 03:09:17 pm »
Actually, by the time you unpack Sage, it will be closer to 5GB.

Are you running Linux?

As for coding the matrix, did you notice the resources page with the code?  Supposedly, the author goes through the down and dirty details of the important operations without even importing the numpy modules.   That alone make me curious.  I am interested in such details ... to possibly "lift" the code into C++.  That $10 ebook version is the way to go if your phone can read it.   I think I will bite the bullet and purchase a hardcopy of Coding the Matrix.  I'll make room on the shelf.  I think it might be worth the investment since the website includes code, slides, and labs. 

I think it would be a good supplement to the text I am using which does not cover any computer science.  The following review decided it for me.  I realize that I am infected with a genuine interest.

Quote from: Former ERIMite
I am am a retired software engineer who spent my entire career working for an aero-space research company. I have a master's degree in mathematics including graduate level linear algebra. And yet, it took me years on the job to relate the books to the problems we were solving. Professor Klein does much of it with this book. For me, it is the perfect mix of abstract mathematics with real physical problems. I deeply regret not having it back then.


Also, I have to learn to loosen up.  I had focused on Python (and Sage) for about 6 months ... obsessively going through many pdf format texts, and then I switched gears back to C++ because it has changed so much with STL and beyond; but I have to get over this rigidity.  I can most likely handle coding in both C++ and Python without risking my brain imploding.  Once a madman, always a madman, I guess.  ::)

It's all interconnected.  Python was written with C/C++ ... and Sage is built upon Python ...

Also, as an aside, one of the first books I read on C programming that really made me want to learn more, back when it was all extremely foreign to me, was written by a physicist who claimed not to be a computer scientist ... and I still feel the influence that reading experience must have had on me, since all I have ever really asked of myself is to be able to write relatively small segments of code that perform some kind of mathematical operation.   I'm not into war games or spreadsheets, charts, or even website development.  Networks fascinate me, using sockets and such, but, like you mention, I don't dedicate all my energy to mastering anything in particular.

I feel compelled to remind you that even though I have been exposed to programming and mathematics for many years, I constantly feel like a beginner.   There is just so much to explore, such as Extending Python with C or C++.   In another thread I tried to articulate something profound, but I did not explain it very well.  It was something to the effect that if one has a desire to explore somewhat sophisticated disciplines, one may have to get used to feeling a bit overwhelmed or just be satisfied with a basic understanding of elementary concepts.  I allow myself to feel "stupid".  It's a human thing, I think.

You see, once again, 10 more years have got behind me.  No one told me when to run.  I missed the starting gun ...

As for Sage, I had attempted to build from sources, but always end up using the binary tarball.

Move it from Download folder to directly in home directory.

tar -xjf sage-7.0-Ubuntu-x86_64.tar.bz2

That places all you need in a directory (in the 7.0 version) called SageMath.

One thing I noticed each time I install a new version is that it is best to place the sage directory in your ~ directory (/home/<username>/SageMath).

Then edit your .bashrc file to include:

export SAGE_ROOT=/home/<username>/SageMath

Also, issue the command:

sudo ln -s /home/<username>/SageMath/sage /usr/local/bin/sage

You may not use Sage constantly, but it is a very cool environment to explore.

From the sage prompt, you can launch Sage Notebook with: notebook()

Generally I use the command prompt mode.  Also, it has SciPy and NumPy and SymPy and more under the hood, but there are some differences.  Whereas in SymPy you create variables with:  x, y = var('x y')

In sage, it is:  x, y = symbols('x, y')

Little differences like that might get on your nerves.  I don't mind.  I appreciate all the work they put into it.

Also, where Python uses x**2, sage uses x^2, which is more intuitive.

You know the way 3/2 reduces to 1 in Python?  Not in sage.  One thing I really like about it is being able to work with actual rational numbers.

Just remember, learning math (and math-related programming) is NEVER a waste of time.   

I really do think that mathematics and programming reinforce each other as disciplines.  I hope you don't get too ****ed off with how gargantuan sage is.  It tries to encompass all the modules, from numpy and sympy to gap, lapack and the rest ...  Hopefully you have room to play with and don't get jammed up.   It's worth exploring.  You might use it as a high-end calculator at the very least.   Think of it as "the anti-capitalists' Mathematica".   :D

Besides ... what else is one to do with one's time in this multiverse?  If one is born into some wealth, shall one build  empire, run for president, give up the life of luxury and inspire a new religion?   Shall we imagine a new mythology with a new kind of hero and a more modern kinds of "monsters"?

I'm afraid that developing our understand is about as good as it gets ...

May you enjoy your higher mental faculties in peace.

time

What now?     ???

what now
« Last Edit: January 26, 2016, 09:15:14 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 ~

Mad Dog Mike

  • { }
  • { ∅, { ∅ } }
  • Posts: 5088
  • Life teaches us not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #23 on: January 28, 2016, 10:48:41 pm »
I have decided not to bother with an ereader.  I am sticking with hardcopy books and reading pdf files with the notebook computer.

NOTE: Please excuse my poor communications.   I am tired and a little grumpy.

I took a look at the code in Recipes for Linear Algebra Computation: LU Factorization Using C++.

It is only available in the Kindle edition, and since there is no Kindle software for Linux, I booted up Windows 10 and set up the Kindle app to read the code ($3).

I of course had to type the code and make some little changes just to compile it.  The ebook claims to use C++ but it is basically C++ using C, which means mostly all pointers.

Tortellini uses pointers for working with matrices, and he stores them in Column Major Order instead of Row Major Order.  Since I tweaked the code to accept input from the console, I really had to roll my sleeves up.  It's a little confusing, but I am gaining on it.  Fortunately I will be getting Coding the Matrix.  Hopefully this will give me some insight.  Actually, I don't think that book attempts LU Factorization.  I will not spend too much time on this code, but I did make enough changes to it where I am sure to be tinkering with it again.

The thing is, not only did the code (based on C++ ebook [LU]) which I was able to organize the matrix properly, compile, and run come up with different results than the textbook, but both sage and scipy came up the same results which were different from the text AND the code I put together in C++.  What the fuuck.   Now it is well after midnight, and I think I have to just let it rest for now. 

Evidently the Devil is in the details. 

There are major inconsistencies, and I would like to understand why this is so.

This is reality.  My goal is to eventually write code that uses C++ a matrix of vectors which itself consists of vectors of doubles, but I am still up to working with pointers for now.  It's just a challenge to follow the code using Column Major Order.  This is how the elements are stored in memory.  It's quite an exercise just debugging with gdb and printing elements with "print *(A+3)" to access A[3], the fourth element.

Anyway, these are the dirty details of working with matrices with computers.

This is surely For Madmen Only!

I am not concerned with how a machine can "recognize" objects.  The technical details of factoring a matrix into lower triangular and upper triangular matrices will suffice to challenge my mind.

 :P

Quote
    Row-major storage traverses the matrix by rows, then within each row enumerates the columns. So our A would be stored in memory as a11, a12, a13, a21, a22, a23. This matches reading order in English and most other Western languages. Row-major storage order for 2D arrays is used by C / C++, the Borland dialects of Pascal, and pretty much any language that was designed since the mid-1970s and supports multidimensional arrays directly. (Several newer languages such as Java don’t support 2D arrays directly at all, opting instead to represent them as an 1D array of references to 1D arrays). It’s also the dominant storage order for images. The position of the element at row i, column j in the underlying 1D array is computed as i*stride + j, where stride is the number of elements stored per row, usually the width of the 2D array, but it can also be larger.
   
Column-major storage traverses the matrix by columns, then enumerates the rows within each column. A would be stored in memory as a11, a21, a12, a22, a13, a23. Column-major storage is used by FORTRAN and hence by BLAS and LAPACK, the bread and butter of high-performance numerical computation. It is also used by GL for matrices (due to something of a historical accident), even though GL is a C-based library and all other types of 2D or higher-dimensional data in GL (2D/3D textures etc.) use row-major.

unrelated to post but relevant to thread:  Linear Algebra: Function [gerardnico.com]
« Last Edit: February 07, 2016, 09:50:06 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 ~

Mad Dog Mike

  • { }
  • { ∅, { ∅ } }
  • Posts: 5088
  • Life teaches us not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #24 on: January 29, 2016, 08:10:16 am »
Note to self:  Gaussian Elimination and LU Factorization to analyze print out of yesterdays code (LU factorization using pointers to store matrices with Column Major Ordering)

Things to consider:

(1) The algorithms themselves

(2) Code that uses partial pivoting may come up with different results than when done by hand (as in textbooks)

(3) It is a worthwhile exercise to consider how matrices are stored in actual contiguous computer memory.

(4) This will also motivate looking at different libraries to see how others have approached the same quandaries

Also take a look at Tested C++ code for the compact LU factorization / decomposition schemes of Crout, Doolittle and Cholesky
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 ~

Mad Dog Mike

  • { }
  • { ∅, { ∅ } }
  • Posts: 5088
  • Life teaches us not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #25 on: January 29, 2016, 10:09:02 pm »
Uncanny?  Philip Klein, the author of Coding the Matrix: Linear Algebra through Computer Science Applications is a professor at Brown University in Providence, Rhode Island ...

Isn't that where HPL lived?

Oh, and I realized the main reason I am more comfortable with hard copy books:  I don't like the idea of being dependent upon batteries and electricity to access texts.

I will wait for ereaders that are solar-powered.   ;)
« Last Edit: January 30, 2016, 10:58:58 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 ~

Mad Dog Mike

  • { }
  • { ∅, { ∅ } }
  • Posts: 5088
  • Life teaches us not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #26 on: January 31, 2016, 05:06:24 pm »
Note:  I am sure it already exists out there, but in the future I would prefer creating a Matrix class using <vectors> and accessing elements with set() and get() methods ...

I guess my mind is all over the place today, and I just want to remind my future self not to spend too much time on the LU Factorization with pointers and matrices in column major order.  It's interesting to see how this is don, but I will want to work with Matrices where the code will be more intuitive.

Also, while going through exercises in Poole text, section 3.4, #11, I noticed an error in the solution manual right in the work ... so I went about checking with sage, the c++, and even scipy.

While sage and scipy come up with the same answers as I do by hand when there are no row exchanges, when there are exchanges, the solutions are different.  So I was doing a search on "inconsistencies with LU factorization" with Sage, and I found yet another free Linear Algebra eBook in PDF recently translated to English, c. 2015 (July): Linear Algebra With Sage

Note:  The chapter on LU-Decomposition, section 3.7, is missing in action.   :(

The authors refer to some nice plots at "geogebra" (DOT org)

The author also provides a link to some examples for each section in the text of working with Sage, and even though the comments there are in Korean, all the mathematics and Sage code is legible, and when you hit the "evaluate button", the output in the Sage notebook is the same ... so, it's not too difficult to follow if you want some of the best examples I've seen for using Sage for Linear Algebra.

« Last Edit: February 01, 2016, 06:05: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 ~

Mad Dog Mike

  • { }
  • { ∅, { ∅ } }
  • Posts: 5088
  • Life teaches us not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #27 on: February 01, 2016, 09:34:33 pm »
I don't know if it is because I have become an old Steppenwolf or not, but there are some things that used to bug me about LU factorization and permutations of rows that don't bother me anymore.

At first, once again, the fact that there doesn't seem to be one unique solution or one specific permutation and factorization of PA = LU alarmed me a little (see above where I am concerned that I was coming up with a different PA = LU than the text [both are valid], and that Sage and scipy.linalg.lu were coming up with the same results as each other, but different from me or the text or the "Tortellini C code".

In the end, I feel most comfortable doing the work with pencil, and then, rather than having a CAS work out the PLU factorization entirely, just checking my LU and PA do ensure than PA - LU = 0.

A note about Coding the Matrix:  this is good as a supplement to another text, such as Poole's ...

Happy computing ...  :-\
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 ~

Mad Dog Mike

  • { }
  • { ∅, { ∅ } }
  • Posts: 5088
  • Life teaches us not to want it.
    • What Now?
Re: An Invitation to Explore ... Linear Algebra?
« Reply #28 on: February 02, 2016, 08:06:27 am »
Finding a basis of the space spanned by the set

I think this is a good example of the kind of "software" I myself am interested in writing, basically. 

From simple procedures to more complicated ones, interfaces that guide a student through processes where they can enter the parameter values, and then have not only the results shown, but each step of the solution displayed.

Of course, I would also want the source code available for inspection of the curious cybernaut who wanted to see the code monkey at work underneath the software ...

One might wonder why I am returning to all this at my age.  It's not like I am planning on becoming an engineer or that I would be trusted to instruct the youth.  Maybe I might find it satisfying to spend the rest of my days on this earth concentrating on "educational software" ... just for the thrill of it.
« Last Edit: February 02, 2016, 08:37: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 ~

Mad Dog Mike

  • { }
  • { ∅, { ∅ } }
  • Posts: 5088
  • Life teaches us not to want it.
    • What Now?
coding the matrix: vector class
« Reply #29 on: February 04, 2016, 02:50:25 pm »
There is a Vec class we are building in Coding the Matrix.   The book does not build the crucial arts of the code but expects the reader to implement vector operations using the given model.  I am tempted to make modifications to the class Vec itself, but I assume there is a method to the madness, so I am trying to resist that impulse. 

There is not much help out there.  You would think there would be some kind of forum for Coding the Matrix.

class Vec:
    """
    A vector has two fields:
    D - the domain (a set)
    f - a dictionary mapping (some) domain elements to field elements
        elements of D not appearing in f are implicitly mapped to zero
    """
    def __init__(self, labels, function):
        self.D = labels
        self.f = function

Testing the implementation with "python -m doctest vec.py":

**********************************************************************
File "vec.py", line 48, in vec.equal
Failed example:
    Vec({'a', 'b', 'c'}, {'a': 0}) == Vec({'a', 'b', 'c'}, {})
Expected:
    True
Got:
    False
**********************************************************************
File "vec.py", line 50, in vec.equal
Failed example:
    Vec({'a', 'b', 'c'}, {}) == Vec({'a', 'b', 'c'}, {'a': 0})
Expected:
    True
Got:
    False
**********************************************************************

You see, an empty dictionary, dict() is {}.

Now, inherently, {'a':0'} does not equal {}, but somehow I am going to have to make Vec(D, {}) a zero vector where all values for each domain member is 0. 

I found someone on the Internet who must also be going through Coding the Matrix, because the code he references is the exact same Vec class.

class Vec:
    """
    A vector has two fields:
    D - the domain (a set)
    f - a dictionary mapping (some) domain elements to field elements
        elements of D not appearing in f are implicitly mapped to zero
    """
    def __init__(self, labels, function):
        self.D = labels
        self.f = function

    __getitem__ = getitem
    __setitem__ = setitem
    __neg__ = neg
    __rmul__ = scalar_mul #if left arg of * is primitive, assume it's a scalar

    def __mul__(self,other):
        #If other is a vector, returns the dot product of self and other
        if isinstance(other, Vec):
            return dot(self,other)
        else:
            return NotImplemented  #  Will cause other.__rmul__(self) to be invoked

    def __truediv__(self,other):  # Scalar division
        return (1/other)*self

    __add__ = add

    def __radd__(self, other):
        "Hack to allow sum(...) to work with vectors"
        if other == 0:
            return self

    def __sub__(a,b):
        "Returns a vector which is the difference of a and b."
        return a+(-b)

    __eq__ = equal

    def is_almost_zero(self):
        s = 0
        for x in self.f.values():
            if isinstance(x, int) or isinstance(x, float):
                s += x*x
            elif isinstance(x, complex):
                s += x*x.conjugate()
            else: return False
        return s < 1e-20

    def __str__(v):
        "pretty-printing"
        D_list = sorted(v.D, key=repr)
        numdec = 3
        wd = dict([(k,(1+max(len(str(k)), len('{0:.{1}G}'.format(v[k], numdec))))) if isinstance(v[k], int) or isinstance(v[k], float) else (k,(1+max(len(str(k)), len(str(v[k]))))) for k in D_list])
        s1 = ''.join(['{0:>{1}}'.format(str(k),wd[k]) for k in D_list])
        s2 = ''.join(['{0:>{1}.{2}G}'.format(v[k],wd[k],numdec) if isinstance(v[k], int) or isinstance(v[k], float) else '{0:>{1}}'.format(v[k], wd[k]) for k in D_list])
        return "\n" + s1 + "\n" + '-'*sum(wd.values()) +"\n" + s2

    def __hash__(self):
        "Here we pretend Vecs are immutable so we can form sets of them"
        h = hash(frozenset(self.D))
        for k,v in sorted(self.f.items(), key = lambda x:repr(x[0])):
            if v != 0:
                h = hash((h, hash(v)))
        return h

    def __repr__(self):
        return "Vec(" + str(self.D) + "," + str(self.f) + ")"

    def copy(self):
        "Don't make a new copy of the domain D"
        return Vec(self.D, self.f.copy())


See Vector Class Problems

The problem is in the equal(u, v) procedure.  It seems the operation overloading depends on this working properly, so I will not be able to proceed through the book until I get this Vec class working flawlessly.

This is one of the great benefits of studying outside the university setting, as I am free to put doing exercises from the main [POOLE] text on hold to put some time into learning obscure features of Python (or C++) at my leisure.
« Last Edit: February 04, 2016, 03:26:41 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 ~