CoCoA:HowTo:Handle Matlab Matrices

From ApCoCoAWiki
Revision as of 14:56, 15 October 2007 by Dheldt (talk | contribs) (Matlab Matrices moved to HowTo:Handle Matlab Matrices: tidying up the wiki...)

Having a matrix in Matlab and load it into CoCoA4 is not a very easy thing to do. First, the representation is slightly different, second you have to adjust Matlab's IEEE notation of floats into a presentation CoCoA is able to read.

This howto explains, how this can be solved.

Exporting from Matlab to ANSI

The Code to create a almost CoCoa-like list of lists from a Matrix M in Matlab is the following:

S = '['; 
for i=1:1:Rows
      if i == 1
          S = [S, '['];
      else
          S = [S, ',['];
      end;	
      for j=1:1:(Columns-1)
          S = [ S, num2str(M(i,j)),','];
      end;
      S = [ S, num2str(M(i,Columns)),']'];
end;
S = [S,']'];

code by dheldt 12:25, 8 Sep 2005 (CEST) (M is a Rows x Columns - matrix)

No we have something like:

M = [[1,0.333333333];[2,25, 0.000000000000004]]

leads to

S = [[1,0.33333],[2.25,4e-015]]

Importing from ANSI into CoCoA

We have to strip the exponential notation into a CoCoA readable format. My suggestion to achieve this, is using a list construction in CoCoa.

We can interpret the e in the exponential representation as an indeterminate in Q[e]. Then the leading term of a number is the mantisse in any way. If there is a second term, we know that this term represent the exponent, so P-LM(P) represents the exponent. If there is only one term P-LM(P) equals 0. So LC(P)*10^(LC(P-LM(P)) multiplies the mantisse (LC(P)) with 10^exponent.

In CoCoAL this looks like

Use Q[e];
S := ... -- put the String S here

K :=  [     
	[ LC(N)*10^(LC(N - LM(N))  )
	| N In P ]
      | P In S];

code by dheldt 12:25, 8 Sep 2005 (CEST)

And now we have a list of list K with valid numbers. For example

S := [[1,0.33333],[2.25,4e-015]];

leads to

K :=  [[1, 33333/100000], [9/4, 1/250000000000000];