Author Topic: Matrices/Complex Numbers  (Read 10308 times)

0 Members and 1 Guest are viewing this topic.

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Matrices/Complex Numbers
« on: June 14, 2012, 02:22:52 pm »
I'm beginning to plan for a library that adds complex numbers and matrices to Lua (both calculator and computer). (There would also be copies that add one or the other because this will probably be a big library that could take a while to load.) I feel like I'm always making simplified versions of these, so I want to just make a complete library and be done with it.

I haven't taken linear algebra yet, but I don't want to have to edit my library later to add new functions, so I need to gather all the operations/features of matrices and complex numbers that would be useful in a multipurpose library. I don't plan on doing much symbolic stuff.

I haven't started coding yet, so suggestions are welcome, but I'll probably add a topic in the Lua section when I get to that point. I'd appreciate help adding to the following list if there's something you think would be useful. I'll get the basic ones out of the way here. If you notice anything that's inaccurate or incomplete, please correct it. :)

Spoiler For One Matrix and One Scalar Operations:
One Matrix and One Scalar Operations:

Spoiler For Addition:
Addition:

Adds the scalar to each element in the matrix

Limitations: none

Example: http://www.wolframalpha.com/input/?i=simplify+%7B%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D%2B5
Spoiler For Subtraction:
Subtraction:

Add the quantity on the left with the quantity on the right multiplied by -1. (see above and below)

Limitations: none

Example: http://www.wolframalpha.com/input/?i=simplify+5-%7B%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D
Spoiler For Multiplication:
Multiplication:

Multiply each element on the matrix by the scalar.

Limitations: none

Example: http://www.wolframalpha.com/input/?i=simplify+5*%7B%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D
Spoiler For Division:
Division:

If the scalar is on the left, take the multiplicative inverse of each element in the matrix and multiply
If the matrix is on the left, divide each element in the matrix by the scalar

Limitations: If the matrix is on the right, it may not contain a 0. If the scalar is on the right, it may not be zero.

Example: http://www.wolframalpha.com/input/?i=simplify+5%2F%7B%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D
Spoiler For Exponentiation:
Exponentiation:

If the scalar is the exponent, repeat matrix multiplication the number of times specified.
If the matrix is the exponent, raise the scalar to the power of each element.

Limitations: If the scalar is the exponent, it must be a nonnegative integer (raising it to the 0th power produces an identity matrix) and the matrix must be a square matrix. If the matrix is the exponent, the base must be nonnegative, and if the base is 0, the matrix may not contain nonpositive numbers.

Examples: http://www.wolframalpha.com/input/?i=simplify+%7B%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D%5E5
http://www.wolframalpha.com/input/?i=simplify+5%5E%7B%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D
Spoiler For Two Matrix, Element-by-Element Operations:
Two Matrix, Element-by-Element Operations:

Spoiler For Equality:
Equality:

If each corresponding element in each matrix are equal, the matrices are equivalent.

Limitations: Matrices must be the same dimensions

Example: http://www.wolframalpha.com/input/?i=simplify+%7B%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D%3D%3D%7B%7B2%2C+4%2C+6%7D%2C+%7B2%2C+4%2C+6%7D%2C+%7B2%2C+4%2C+6%7D%7D%2F2
Spoiler For Addition:
Addition:

Adds the corresponding elements of the two matrices.

Limitations: Matrices must be the same dimensions

Example: http://www.wolframalpha.com/input/?i=simplify+%7B%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D+%2B+%7B%7B2%2C+3%2C+4%7D%2C+%7B2%2C+3%2C+4%7D%7D
Spoiler For Subtraction:
Subtraction:

Subtracts each element from the matrix on the right from the corresponding element in the matrix on the left.

Limitations: Matrices must be the same dimensions

Example: http://www.wolframalpha.com/input/?i=simplify+%7B%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D+-+%7B%7B2%2C+3%2C+4%7D%2C+%7B2%2C+3%2C+4%7D%7D
Spoiler For Multiplication:
Multiplication:

Multiplies the corresponding elements of the two matrices.

Limitations: Matrices must be the same dimensions

Example: http://www.wolframalpha.com/input/?i=simplify+%7B%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D+*+%28%2B%7B%7B2%2C+4%2C+6%7D%2C+%7B2%2C+4%2C+6%7D%2C+%7B2%2C+4%2C+6%7D%7D%29
Spoiler For Division:
Division:

Divides each element from the matrix on the left by the corresponding element in the matrix on the right.

Limitations: Matrices must be the same dimensions and the matrix on the right must not contain a 0.

Example: http://www.wolframalpha.com/input/?i=simplify+%7B%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D+%2F+%28%2B%7B%7B2%2C+4%2C+6%7D%2C+%7B2%2C+4%2C+6%7D%2C+%7B2%2C+4%2C+6%7D%7D%29
Spoiler For Exponentiation:
Exponentiation:

Raises each element from the matrix on the left to the power of the corresponding element in the matrix on the right.

Limitations: Matrices must be the same dimensions. If the matrix on the left contains any negative numbers, the corresponding element in the matrix on the left must be an integer power (since this is all Lua can handle). If it contains a zero, the corresponding element must be positive.

Example: http://www.wolframalpha.com/input/?i=simplify+%7B%7B1%2C+2%2C+5%7D%2C+%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D+**+%28%7B%7B2%2C+4%2C+6%7D%2C+%7B2%2C+4%2C+6%7D%2C+%7B2%2C+4%2C+6%7D%7D%29
Spoiler For Special Two Matrix Operations:
Special Two Matrix Operations:

Spoiler For Multiplication:
Multiplication:

The elements of the rows in the matrix on the left are multiplied with corresponding columns in the matrix on the right. Then they are added up and that is the element of the new matrix. This element is in the same numbered row as the row used from the matrix on the left and the same numbered column as the column used from the matrix on the right. It can be thought of as the dot product of the row vector and the column vector.

Limitations: The number of columns in the matrix on the left must be equal to the number of rows in the matrix on the right.

Example: http://www.wolframalpha.com/input/?i=simplify+%7B%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D+*+%7B%7B2%2C+2%2C+2%7D%2C+%7B3%2C+3%2C+3%7D%2C+%7B4%2C+4%2C+4%7D%7D
Spoiler For One Matrix Operations and Properties:
One Matrix Operations and Properties:

Spoiler For Transpose:
Transverse:

For each element in a matrix, switch its row number with its column number. Switch the dimensions, also.

Limitations: none

Example: http://www.wolframalpha.com/input/?i=simplify+%7B%7B1%2C+2%2C+5%7D%2C+%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D%5ET
Spoiler For Conjugate Transpose:
Conjugate Transpose:

The same as the transpose except it switches the sign on any imaginary values in the matrix. (6+4i becomes 6-4i). If all the elements in the matrix are real, then the conjugate transpose is the same as the transpose.

Limitations: none

Example: http://www.wolframalpha.com/input/?i=simplify+conjugate+transpose+%7B%7B1%2C+2%2B4i%2C+5i%7D%2C+%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2-i%2C+3%7D%7D
Spoiler For Pseudoinverse:
Pseudoinverse:

Does this stuff http://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_pseudoinverse. It's a general inverse for all matrices regardless of their dimensions. The result of the product of the original and the pseudoinverse isn't necessarily the identity matrix, but if multiplied by the original matrix again, it will produce the original matrix. It always exists and is always unique. If the matrix is a non-singular square matrix, the pseudoinverse will be equal to the inverse (there is still a pseudoinverse for singular square matrices).

Limitations: none

Example: http://www.wolframalpha.com/input/?i=pseudoinverse+%7B%7B1%2C+2%2C+5%7D%2C+%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D
Spoiler For Determinant:
Determinant:

The determinate has various geometric properties. The determinate of an inverse matrix is the the multiplicative inverse of the determinate of the original matrix. If the original determinate is zero, the matrix is singular.

Limitations: The matrix must be a square matrix

Example: http://www.wolframalpha.com/input/?i=simplify+determinant+%7B%7B1%2C+2%2C+5%7D%2C+%7B1%2C+2%2C+3%7D%2C+%7B1%2C+2%2C+3%7D%7D
Spoiler For Generatable Matrices:
Generatable(?) Matrices:

Spoiler For Identity Matrix:
Identity Matrix:

Generates a square matrix that has all 0s except for along the diagonal where it has 1s.

Limitations: It must be a square matrix (implying that it's dimensions will be positive integers).

Example: http://www.wolframalpha.com/input/?i=simplify+identity+matrix+5

Spoiler For Random Matrix:
Random Matrix:

Generates a matrix with specified dimensions and random elements between 0 and 1. (Other operations can be done if you want a different range or complex numbers)

Limitations: Its dimensions must be positive integers.

Example: http://www.wolframalpha.com/input/?i=random+6x2+matrix

Other functions:
ref()
rref()

There is also all the other various math functions. They all take one matrix as an argument and effect each element-by-element.

As far as complex numbers go, are there any useful functions that apply to complex numbers (cis and arccis for example). I would update the math library to include complex numbers too. I don't know what to do about the periodicity created by some operations, yet.  :-\

 :D  The End          (It's only the beginning) x.x
« Last Edit: June 15, 2012, 01:54:11 pm by 3rik »
Userbars

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: Matrices/Complex Numbers
« Reply #1 on: June 14, 2012, 09:17:40 pm »
Hmm. This will be interesting. Good luck.  :D

This reminds me of one algorithm of multiplying 2 2*2 matrices that requires only 7 multiplications instead of 8.

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: Matrices/Complex Numbers
« Reply #2 on: June 14, 2012, 10:46:57 pm »
I found that algorithm. http://en.wikipedia.org/wiki/Strassen_algorithm

I think the extra calculations may actually slow it down in Lua because of all the adding that goes on. I can see how things like servers could benefit from the optimization, especially if they're in a low level language, but I'll probably just stick with the common algorithm.

I had never heard about that algorithm before, though. Thanks :)
« Last Edit: June 14, 2012, 10:47:15 pm by 3rik »
Userbars

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: Matrices/Complex Numbers
« Reply #3 on: June 14, 2012, 10:56:31 pm »
Cant you add and subtract two matrices?

EDIT: nvm have to learn to read all of a post before replying :P
you might want to put the wo Matrix, Element-by-Element Operations with the Two Matrix Operations
« Last Edit: June 14, 2012, 10:57:41 pm by ruler501 »
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: Matrices/Complex Numbers
« Reply #4 on: June 14, 2012, 11:07:47 pm »
Thanks for pointing out that confusion. I reorganized the categories so that it might be less confusing.
Userbars

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: Matrices/Complex Numbers
« Reply #5 on: June 15, 2012, 12:23:47 am »
I'm not advanced enough in math to tell you much more about complexes then the conjugate though from the conjugate transpose I'd guess its already planned/done
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Matrices/Complex Numbers
« Reply #6 on: June 15, 2012, 01:43:16 am »
i would like this :)

although a few suggestions:
  • dividing matrices doesn't really exist.. you have to do this: A*B = C*D  -->  A-1*A*B = A-1*C*D  -->  B = A-1*C*D
  • your equality should be more advanced. rows are multiplications with a scalar, then they're equal too
  • check if they're consistent by solving them
  • (row) reduced echelon form could come in handy, this can be done with math.eval i think (but i really don't know what or how it will return then)
  • A = L*U
  • A = Q*R
  • determinant
  • adding a scalar to a matrix doesn't work either. You have to add the scalar*identinty
  • ^this for substraction
  • checking for inversability (this can be done in like 25 ways or something, so it should be possible xp )

that's about it. I know this is a huge list, but it's only to help you :) if you need help i would be glad to help you, i had an exam of lineair algebra last week, so it's still in my mind now :)

edit: there are still a lot different things i would love to see, but you can start doing these operations xp
L*U and Q*R are special operations which dissolve (?) a matrix into 2 matrices, each with a special format
« Last Edit: June 15, 2012, 01:48:19 am by Nick »

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: Matrices/Complex Numbers
« Reply #7 on: June 15, 2012, 09:26:26 am »
dividing matrices doesn't really exist..
I'm aware that it isn't formally defined. A secondary goal of this library is to provide some abstraction for somewhat large amounts of data. It falls under more of the applied math side of the spectrum rather than pure math. The pseudoinverse would work fine for the operation you're talking about. (The element-by-element division would have a different result as multiplying by the inverse/pseudoinverse)

your equality should be more advanced. rows are multiplications with a scalar, then they're equal too
I'm a bit confused about this one. I'm pretty sure I've seen definitions where the dimensions must be the same and each pair of corresponding elements must be equal for the matrices to be equal. I could add something that checks if the matrices are proportional if that's what you meant.

check if they're consistent by solving them
I'm not really sure I understand this one.

(row) reduced echelon form could come in handy, this can be done with math.eval i think (but i really don't know what or how it will return then)
A = L*U
A = Q*R
I don't know what these are, but I'll look into it.

determinant
I have this one already under One Matrix Operations and Properties

adding a scalar to a matrix doesn't work either. You have to add the scalar*identinty
^this for substraction
I know in pure math you would have to do that. It's more for convenience. If someone were keeping track of a bunch of data and wanted to shift them all by a constant, they could use this as a shortcut. (It's just a faster way of your method, even if it is technically wrong mathematically). Would it be better to describe it as a shift? The TI-84+ allows it while the nspire doesn't.

checking for inversability (this can be done in like 25 ways or something, so it should be possible xp )
This would be available with the determinant. It might look like
Code: [Select]
matrix.det(matrixname)~=0 --same as isinvertable(matrixname)
Thanks a lot for this feedback. :D I'm just gathering some ideas right now, so if you have more ideas I'd love to hear them. There's just too much information out there.
« Last Edit: June 15, 2012, 09:40:14 am by 3rik »
Userbars

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Matrices/Complex Numbers
« Reply #8 on: June 15, 2012, 12:45:19 pm »
ok, glad you found it helpful.

if you need some more, don't hesitate :) and i see you're going the more practical way, rather than the pure mathematics way, i like that, but make sure the difference can be seen between the practical and the theoretical
e.g. if you do matrix = matrix + 5, how could you see if it's matrix + 5*identity or every element + 5 ... that's what i mean

i could write you a small guide on how to handle the rref (row reduced echelon) and the A = Q*R and A = L*U (although these are quite advanced, but they should be in it, it's the basics of matrix calculations, especially rref and A=Q*R)

edit: about those things you didn't fully understand:
a matrix that is consistent is a matrix that is seen as a system (?) of more than one equation with multiple variables (optimal is the same #equations as #variables)
that way you can get the value of each variable in the set of equations
e.g.
A+B = 2
2A-3B = 0
^this is a set of equations that can be calculated by substitution. if you solve it you get a = 6/5 and b=4/5.
if the combination of equations is possible it is 'consistent', else it is not
e.g.
A + 2B = 0
A + 2B = 1
^this is not consistent, since that combination is impossible

now such a set of equations can be represented in a matrix as follows:

A+B = 2
2A-3B = 0

is the same as

1   1   2
2  -3  0

got it?
if you take the rref() of this matrix, you will get the following result:
1   0   6/5
0   1   4/5

which gives you that A=6/5 and B=4/5
« Last Edit: June 15, 2012, 12:52:15 pm by Nick »

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: Matrices/Complex Numbers
« Reply #9 on: June 15, 2012, 01:45:53 pm »
Wow! rref is useful! :D I'll add that to my list for sure.

Right now I just need to sort through all the matrices stuff on the internet, so any help sorting out the things that are helpful and the things that take up space. I'll worry about how I'll do things later. I only need a list of things that are possible right now.
Userbars

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Matrices/Complex Numbers
« Reply #10 on: June 15, 2012, 01:48:37 pm »
you upper list was quite complete for the basics i think (not to discourage you, but matrices are a huuuuge part of algebra, which means a lot of functions/other things to add)

the most important are the ones in the first post, and:
  • ref()
  • rref()
  • invert

that'll be enough to start xp

here is something i just tested and can be quite useful :)

Code: [Select]
function on.create()
a = {{1,1,2},{2,-3,0}}
b = math.eval("rref({{1,1,2},{2,-3,0}})")
end

function on.paint(gc)
gc:drawString(b[1][1]..","..b[1][2]..","..b[1][3],0,0,"top")
gc:drawString(b[2][1]..","..b[2][2]..","..b[2][3],0,20,"top")
end
« Last Edit: June 15, 2012, 01:58:49 pm by Nick »

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Matrices
« Reply #11 on: July 01, 2012, 01:48:30 pm »
I'm at the point in programming where I need to program the LU decomposition, except I have no idea how to do that. I understand the basic concept of it; find lower and upper triangle matrices that produce the original matrix. However, since I haven't taken linear algebra yet, there are some parts that either I don't understand or I'm unfamiliar with their notation. Also, there are references to diagonal and pivot matrices. Which method is most useful in applications. I would also like a method that works for the most matrices possible. Any help would be appreciated.
« Last Edit: July 01, 2012, 01:49:16 pm by 3rik »
Userbars

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Matrices/Complex Numbers
« Reply #12 on: July 02, 2012, 05:52:01 pm »
We can use a calculator for math too?? O.O

(sorry, I couldn't resist after having to delete games from my PRIZM due to running out of memory)
« Last Edit: July 02, 2012, 05:53:09 pm by DJ_O »

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: Matrices/Complex Numbers
« Reply #13 on: July 02, 2012, 06:36:23 pm »
We can use a calculator for math too?? O.O

Or math can be used to make games.  ;)
Userbars

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
Re: Matrices/Complex Numbers
« Reply #14 on: July 02, 2012, 06:37:25 pm »
like cannon fodder on paper! That can be fun