Difference between revisions of "CoCoA:HowTo:Handle Matlab Matrices"

From ApCoCoAWiki
m (Andraschko moved page HowTo:Handle Matlab Matrices to CoCoA:HowTo:Handle Matlab Matrices: not relevant for ApCoCoA-2)
m (Bot: Replacing category HowTo with HowTo Old)
 
Line 71: Line 71:
 
  K :=  [[1, 33333/100000], [9/4, 1/250000000000000];
 
  K :=  [[1, 33333/100000], [9/4, 1/250000000000000];
  
[[Category:HowTo|{{PAGENAME}}]][[Category:ApCoCoA|{{PAGENAME}}]]
+
[[Category:HowTo Old]]
 +
[[Category:ApCoCoA|{{PAGENAME}}]]
 
[[Category:CoCoA4|{{PAGENAME}}]]
 
[[Category:CoCoA4|{{PAGENAME}}]]

Latest revision as of 09:42, 29 October 2020

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 := [];

ForEach P In S Do
	L := NewList(0);
	ForEach N In P Do
		If Not(N=0) Then
			Append(L, LC(N)*10^(LC(N - LM(N))) );
		Else
			Append(L, 0);
		EndIf;
	EndForEach;
	Append(K,L);
EndForEach;

-- outdated version:
--K :=  [     
--	[ LC(N)*10^(LC(N - LM(N))  )
--	| N In P ]
--     | P In S];
-- this code does not work with 0 in S, because LM(0) is not defined!

code by dheldt 12:25, 8 Sep 2005 (CEST), updated by dheldt 18:15, 28 November 2007 (CET)

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];