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

From ApCoCoAWiki
m
(→‎Importing from ANSI into CoCoA: updated the cocoa code to translate something like 4e-10 to support also 0 as input)
Line 43: Line 43:
 
  Use Q[e];
 
  Use Q[e];
 
  S := ... -- put the String S here
 
  S := ... -- put the String S here
 +
 
 +
K := [];
 
   
 
   
  K :=  [     
+
  ForEach P In S Do
[ LC(N)*10^(LC(N - LM(N))  )
+
L := NewList(0);
| N In P ]
+
ForEach N In P Do
      | P In S];
+
If Not(N=0) Then
code by [[User:Dheldt|dheldt]] 12:25, 8 Sep 2005 (CEST)
+
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 [[User:Dheldt|dheldt]] 12:25, 8 Sep 2005 (CEST), updated by [[User:Dheldt|dheldt]] 18:15, 28 November 2007 (CET)
  
 
And now we have a list of list K with valid numbers. For example  
 
And now we have a list of list K with valid numbers. For example  

Revision as of 17:15, 28 November 2007

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