Menu

#82 vectors and matrixes

Next_Release
open
nobody
None
1
2021-08-24
2021-08-03
Stefano
No

Hi,

it would be nice if there were functions to create identity matrixes, or ones, or zeros based only on the given dimension; for example :

ones(3) = {1;1;1}
zeros(2) = {0;0}
identity(2) = { 1 ; 0
                                              0 ; 1 }

and also it would be useful then to have a function that return the size of a given element; for example:

P = {1; 2; 3}
length(P) = 3
size(P) = {3; 1}

this two tools would be handy to deal with vectors and matrixes in a more automatic way.

Personally I use vectors to store variadic argument, and then use vector operations to get results I need. For example let it be vec y a vector that store the y coordinate of some points:

vec y = {y_1;y_2;y_3;...;y_n}

If want to calculate the sum of (y_i)^2, it is incredibly easy, I just need to do the multiplication (vec y)^T * (vec y) and get a neat result. Coversely if I need just the sum of y_i it's more awkward since i would need to multiply the transpose of vec yfor a vectors of ones of the same size. If I could create this vectors of ones on the fly, based simply on the size of vec y, then I could change the number of element of vec y and automatically have a correct result without the need to manually create a vector of ones of the same size.

Further operation would be possible and would require less manual input.

Really is just a convoluted way to have for each loops in my odt.

Discussion

  • Jan Rheinlaender

    That is a good suggestion. The symbolic math package which iMath uses (GiNaC) has the following functions which it would be easy to implement in iMath:

    • diag_matrix() constructs a square diagonal matrix given the diagonal elements
    • unit_matrix() creates an 'x' by 'x' (or 'r' by 'c') unit matrix
      • unit_matrix(3,1) would be identical to ones(3)
      • unit_matrix(2) would be identical to identity(2)
      • unit_matrix(2,1)-unit_matrix(2,1) would be identical to zeros(2)
    • symbolic_matrix() constructs a matrix filled with newly generated symbols made of the specified base name and the position of each element in the matrix
    • sub_matrix() takes a row offset 'r' and a column offset 'c' and takes a block of 'nr' rows and 'nc' columns
    • reduced_matrix() has two integer arguments that specify which row and column to remove
    • determinant() is already implemented in iMath
    • charpoly() is already implemented in iMath
    • trace()
    • rank()
    • solve() solves a linear system
    • inverse() inverts a matrix
    • rows() gives the number of rows of a matrix
    • cols() gives the number of columns of a matrix

    Would these functions cover all the operations you require?

     
  • Stefano

    Stefano - 2021-08-22

    That would be a great addition! For sure it would cover the use case I was thinking of, and then some more.

     
  • Stefano

    Stefano - 2021-08-24

    So I did some test and found some troubles with identity and inverse matrix.

    An inverse matrix obtained my the function invertmatrix() doesn't behave properly when evalueted, but a manually inserted inverted matrix works fine.

    I also found a weird behavior where the first printval statement doesn't evaluate correctly, but the following copies of the same printval statements instead evaluate correctly.

    Have a look at the attached odt file (also pdf version of my output).

     

    Last edit: Stefano 2021-08-24
    • Anonymous

      Anonymous - 2021-08-24

      Hi Stefano, thanks for your tests.

      I think most of your problems are caused by the fact that you did not announce to iMath that A and B are matrices:
      %%ii MATRIXDEF A
      %%ii MATRIXDEF B

      Thus A and B are treated as normal symbols, which explains A A^{-1} = 1.

       
      • Stefano

        Stefano - 2021-08-24

        you are correct.

        I declared both matrices as you said and now it works fine.

        Just to be clear, I need to declare beforehand even the matrices that are created by a function, like B=invertmatrix(A), otherwise imath doesn't now how to handle them correctly. Anything else was an happy accident.

         
  • Stefano

    Stefano - 2021-08-24

    I am curious: why do you have to rename the ginac function?

    For example I see that rows has become matrixrows and so on.

     
    • Anonymous

      Anonymous - 2021-08-24

      I'm just trying to avoid future name clashes. Also, names with underscore print badly in Office.

       

Anonymous
Anonymous

Add attachments
Cancel