Arbitrary size matrices in maxima - matrix

I want to do some calculations with matrices of arbitrary size. Simple example - take two matrices NxM and MxK, with arbitrary elements, and see element of product as sum.
But i cant find a way to do such symbolic calculations without specifying matrix size as integer.
matrix() want integer, makelist() want integer.
Is there a way to do things like this in maxima? Or any CAS?

Unfortunately, Maxima does not know about arbitrary-size matrices, and I don't see an easy way to implement it.
The only way that I see is to define a new kind of expression, and provide simplification rules for operations on them. E.g. (and this is just a sketch of a possible solution): use defstruct to define a structure comprising size and a formula for a typical element, and define a simplification rule for "." (noncommutative multiplication) which creates a new expression with a typical element which is a summation.

Related

Matrix multiplication using Prolog arrays

It might not be evident, but Prolog also offers arrays out of the box. A Prolog compound has a functor and a number of arguments. This means we could represent an array such as:
[[1,2],[3,4]]
Replacing the Prolog lists by the following Prolog compounds:
matrice(vector(1,2), vector(3,4))
The advantage would be faster element access from an integer index. Can this representation be used to realize a matrix multiplication?
There is yet another approach, as implemented in R (the statistical environment). The dimensions of the array and the values are kept separately. So your square could also be represented as:
array(dims(2, 2), v(1,2,3,4))
This approach has some (questionable) benefits and drawbacks. You can start reading here, if you are at all interested: https://stat.ethz.ch/R-manual/R-devel/library/base/html/dim.html
To your question, yes, you can implement matrix multiplication, regardless on how you decide to represent the matrix. It would be interesting to see how the two approaches (array of arrays vs. one array and calculating indexes from the dimensions) compare in terms of efficiency.
What algorithm do you want to use for the matrix multiplication? Is it any of the ones described here: https://en.wikipedia.org/wiki/Matrix_multiplication_algorithm?
EDIT: do you want to allow the client code to be able to provide the product and sum operations? Do you want to allow specialization of the values? For example, if you want to use matrix multiplication for finding the transitive closure of a graph, you could represent the boolean square matrix as an unbounded integer. This will make the matrix itself at least quite small.

Perform matrix algebra without specifying Matrix elements, but using the matrix objects as elemental

Is there any functionality in Mathematica that let's the user work directly with matrix objects (potentially of non-defined size), eg. solving a matrix equality for the matrix object without necessarily specifying all elements of the matrix. What I want to do is to manipulate matrix equations while using the matrix objects as elemental, so eg solving a matrix equation for a matrix object (and not for all its elements explicitly.
As a simple example of what I mean: say I'd want to check if two matrix inequalities are equivalent eg. and let's say there is a function matrix[A] that declares that A may be of dimension >2.
matrix[A]
matrix[B]
matrix[C]
In principle that would have to be like an element function, like:
element[A, Reals[dim=n]]
Then there should be a function MatrixSolve[] such that
In: Assuming[A is square and det[A]==0, MatrixSolve[A*B==C,B]]
Out:B->A^(-1)*C.
Or for example:
Q:=A*B *so Q must also be a matrix*
In: Assuming[again the necessary stuff like A square..., Q*A^(-1)===B]]
Out: True
I have not found any such functionalities in the documentation (or in the search function on SE) and was wondering if there is a way to implement this and, if not, why such features would not exist.

LAPACK: Triangular system of equations with multiple right hand side

I have my beuatiful triangular n x n matrix, say L (for lower triangular), and I want to solve a system like
LX=B
Where B and X are n x k matrices (that is: I want to solve a triangular linear system with multiple right hand side). Additionally, I have my triangular matrix stored in PACKED FORMAT; i.e. I only store the lower triangular part. I am using BLAS and LAPACK, but I have realised that there is no specific way of solving my problem. Although there are many functions that solve similar problems:
stpsv(): Takes a triangular matrix in packed format and solves for a single right hand side.
strsm(): Takes a triangular matrix in dense format and solves for multiple right hand side.
What I really need is a combination of both. I would like to have a function accepting packed triangular format, as in stpsv(), and also accepting multiple right hand side, as in strsm(). But it seems as if there is not such a function readily available.
So my questions are:
Is there any function that can accept a packed triangular matrix and solve for multiple right hand side?
If the answer is NO, what would be more efficient? Either I call stpsv() in a for loop for every column in B, or I create a dense matrix from L, so that I have all those useless zeros in there and then I call strsm(). What would be better? Moreover, maybe I am missing a more clever way of doing all this.
Packed storage implies BLAS2 routines. Otherwise BLAS3 functions are more efficient in solving linear systems but they work in optimal blocked algorithms. If you call BLAS2 functions then you basically go back to vectored version hence it won't make too much sense.
Note that BLAS2 versions also do not perform conditioning checks. So they are directly optimized for BLAS2 performance since a triangular matrix with single RHS is a direct backward substitution.
For multiple RHS you can convert your matrix via, say stpttr and then use strtrs.
Yes, there is a function to solve Ax=B, for packed triangular matrix A and multiple right hand size B. It is stptrs() from LAPACK. In addition, there are other routines for triangular packed matrices, all featuring tp in their name according to the naming conventions of LAPACK.
However, looking at the source unveils that this function calls stpsv() from BLAS in a loop, once for each right hand side. It's exactly what you suggested!

Rotational invariant hash function for binary matrix

I am looking for a hash function that will assign a scalar value for a small binary matrix(7x7). I want it to give different values for 2 different matrices unless one matrix is a 90 degree,180 degree or 270 degree rotation of the other one.
Do you have any suggestions on how I could do this? I was expecting to find a method in image processing as this would be equivalent to a 7x7 binary image but I could not find anything.
Converting my comment to an answer:
If you're trying to find a way to test if two objects are equivalent after doing some sort of transformation, it often helps to pick a single "canonical form" for the object that can easily be computed. In your case, it would probably help a lot to pick a single rotation of the matrix as the "canonical" rotation and compare things that way. One simple option would be to pick the lexicographically first matrix out of all the rotations possible, then use that.

Evolving a matrix using a genetic algorithm

I recently discovered genetic algothims and after doing a little research I can't find any example on how to evolve structures more complex than a vector or a string.
Let's say that I'm using a covariance matrix for a certain computation (to compute a mahalanobis distance for example) and I want to look for a better matrix to do the job and linimize a certain criteria, are there any classic examples on how to evolve the matrix and which crossover operators to use ?
Thanks !
Any structure of fixed size and shape that is made of numbers (or any other elements) can be rewritten to a 1-D vector and back. You can then use any operator you like which works on vectors.
If you wanted to work with matrices (or any other structures) directly you can always design your own operators, but a matrix basically is a vector, just written in a different way. For the matrix case there are a number of possibilites of operators (crossover):
Swap rows/columns (between the parents)
Swap submatrices (generalization of the above)
Continuous-space crossover methos like BLX-alpha, PCX, arithmetic crossover... These all are designed for vectors but you will just treat the matrix as a vector (it's really not that different).
Mutation is probably going to be more or less identical to the vector-like - you just mutate the elements (or some of them).

Resources