Skip to first unread message

ebarbosa

unread,
Aug 11, 2015, 9:54:05 AM8/11/15
to MIT App Inventor Forum
Hi! 
I'm making an app for matrix operations. I have two matrices stored in the database, each element stored in an index.

How can I make the sum of the elements of these matrices using the for each number block, so that the iteration will be made according the indices in which each element was stored in the database?  
I use a single screen, then I have stored matrices with tags matriz1 and matriz2. Thinking of 2X2 matrices, I have:

Matrix 1 (tag = matriz1)
a11 = index 1
a12 = index 2
a21 = index 3
a22 = iindex 4

Matrix 2 (tag = matriz2)
a11 = index 1
a12 = index 2
a21 = index 3
a22 = index 4

Thanks.


SteveJG

unread,
Aug 11, 2015, 10:58:53 AM8/11/15
to MIT App Inventor Forum
Here is a Pascal algorithm showing how to multiply two matrices  from  http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_20000683.html  by Helge Lorenze.  It does not sum the matrices as you would like.  The Pascal code could be used as a pseudocode example.  The arrays referred to are essentially Lists of Lists.

procedure TForm1.Button1Click(Sender: TObject);

const r1 =3;
      r2=3;
      c1=1;
      c2=3;
var mat1 : ARRAY [1..3, 1..3] OF INTEGER;
    mat2 : ARRAY [1..3, 1..3] OF INTEGER;
    result : ARRAY [1..3, 1..3] OF INTEGER;

// r2 has to be equal to c1 !
// r1 has to be equal to c2 !
// will work for r,c > 3 too;
var r,c,i : integer;
begin
  for r := 1 to r1 do
    for c := 1 to c2 do
    begin
      result[r,c] := 0;
      for i := 1 to c1 do
        result[r,c] := result[r,c] + mat1[r,i]*mat1[i,c];
    end;
end;

If you try to port the code you will be able to multiply...so you need also to change the code to add.

Professional languages usually have access to libraries to help with this type of mathematics.  App Inventor does not.

You probably already know how to manually add or subtract matrices (for those who do not, here is a nice sample http://www.purplemath.com/modules/mtrxadd.htm  of what is required).

Would I want to do matrix addition with Blocks Eliana?   No.   Is matrix addition possible? perhaps.   Abraham or someone who has done this might comment with a neat solution.

I believe this can be done, you will have to do a lot of experimenting.  When programming for Windows, I just would use a matrix library.  The routine to program this using a script is involved in any programming language.

Regards,
Steve









Abraham Getzler

unread,
Aug 11, 2015, 9:18:19 PM8/11/15
to MIT App Inventor Forum
See this link for how to work with lists ...

Matrix addition is much easier than matrix multiplication.

You stored each matrix as a list of 4 elements.

Just run an index i from 1 to the length of your list (4),
and add the sum of the corresponding elements of your lists A(i)+B(i)
to an initially empty list to get your sum list.

Read up on lists of lists.
They are a slightly more natural way to store two dimensional matrices.

ABG




ebarbosa

unread,
Aug 12, 2015, 10:19:10 PM8/12/15
to mitappinv...@googlegroups.com
Thanks, Steve!
I looked the Pascal algorithm, I had also seen an example developed in C, and I based him to begin.
Yes, I even did an example manually, adding the elements by the indexes that were stored in the database. But now I need the ideal.
You meant that the App Inventor doesn't greatly facilitates matrix calculations, unlike other programming languages, right?

Abraham, add it's even easier. But in the end application, I need to addition, subtraction, multiplication, determinants ..

I read about lists. But still it did not work completely. In the image below, only the calculation of the last sum, the last element of matrix1 with the last matrix2 element is being made (12). This means that the iteration is being made, but then how to display the results of each sum, a list with 4 elements (which would be the C matrix)?



I appreciate all contributions.

Abraham Getzler

unread,
Aug 12, 2015, 11:44:46 PM8/12/15
to MIT App Inventor Forum
See the attached for how to use lists of lists as matrices,
and how to do a sum.

This should work with pairs of similar matrices of any size and dimensions.

ABG

sum.png
test of sum.png
globals.png
matrix.aia

SteveJG

unread,
Aug 13, 2015, 11:09:51 AM8/13/15
to MIT App Inventor Forum
You might find this article about using Javascript to do matrix operations   http://mathjs.org/docs/datatypes/matrices.html   . The article describes a Javascript 'library' to do matrix operations.  You asked "You meant that the App Inventor doesn't greatly facilitates matrix calculations, unlike other programming languages, right?".   Yes, very right.  At the moment, AI2 is mathematically crippled lacking the libraries used in professional compilers like Eclipse or Android Studio.  AI2 is a poor choice at the moment for doing many of the things required in scientific or math intensive apps.   Once you are successfuly with an algorithm build on AI2, you have another issue, your code is not portable...you can not cut and paste; the best you can do is to provide a new screen in an app that contains the matrix algorithms, then pass the calculation results to your main app.  Awkward, ugly and I difficult.

I also expect matrix manipulations will be s l o w using AI2 because of the many operations required.    In a few words, AI2 stinks for anything Math intensive, however, it does allow non-Java programmers to do neat things.  Pretty good for an entry level Android compiler primarily intended for teaching programming concepts.


I do not know whether it is possible to use the information supplied in this interesting article within AI2.  Javascript can be used to do some mathematical operations in AI2.  Taifun has several examples...you may have to search .. and his examples do not include matrices.

Regards,
Steve

ebarbosa

unread,
Aug 15, 2015, 11:23:14 AM8/15/15
to mitappinv...@googlegroups.com
Thanks for help, Abraham!
Now I will work with other operations, and I'll try an algorithm to work with matrices of different dimensions.

Steve, thanks for the link.
I understand, the AI2 has some limitations on the development of math intensive applications. I have seen some of these limitations, and I am analyzing the AI2 capabilities for the development of this type of application.

In general, A2 seems a good tool to use. I believe that with the planned improvements, it can become better for developing applications related to mathematics.

Ok, it's really interesting find an alternative to solve the limitations found, I can search about it.

Thank you for all the attention.

Reply all
Reply to author
Forward
0 new messages