b8528395f1fbb195409cc2ca40f59aee3740a1b8
[scilab.git] / scilab / modules / m2sci / macros / sci_files / sci_eig.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2002-2004 - INRIA - Vincent COUVERT 
3 // 
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution.  The terms
7 // are also available at    
8 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
9
10 function [tree]=sci_eig(tree)
11 // M2SCI function
12 // Conversion function for Matlab eig()
13 // Input: tree = Matlab funcall tree
14 // Ouput: tree = Scilab equivalent for tree
15 // Emulation function: mtlb_eig()
16
17 // eig(A)
18 if rhs==1 then
19   // Because %c_spec and %b_spec are not defined
20   A = getrhs(tree)
21   A = convert2double(A)
22   tree.rhs=Rhs_tlist(A)
23
24   // d = eig(A)
25   if lhs==1 then
26     tree.name="spec"
27     tree.lhs(1).dims=list(A.dims(1),1)
28     tree.lhs(1).type=Type(Double,Unknown)
29   // [V,D] = eig(A)
30   else
31     set_infos(msprintf(gettext("mtlb_eig() called because Scilab and Matlab value do not always match for %s."),tree.lhs(1).name),0)
32     tree.name="mtlb_eig"
33     tree.lhs(1).dims=list(A.dims(1),A.dims(1))
34     tree.lhs(1).type=Type(Double,Unknown)
35     tree.lhs(2).dims=list(A.dims(1),A.dims(1))
36     tree.lhs(2).type=Type(Double,Unknown)
37   end
38 // eig(A,B) or eig(A,'nobalance')
39 elseif rhs==2 then
40   [A,B] = getrhs(tree)
41   
42   // 'nobalance'
43   if B.vtype==String then
44     no_equiv(gettext("''nobalance'' option, IGNORED."))
45     tree.rhs=Rhs_tlist(tree.rhs(1))
46     if lhs==1 then
47       tree.name="spec"
48       tree.lhs(1).dims=list(A.dims(1),1)
49       tree.lhs(1).type=Type(Double,Unknown)
50     else
51       tree.rhs(1)=Operation("+",list(tree.rhs(1),Variable("%i",Infer()),list()))
52       rhs2=Operation("+",list(Cste(1),Variable("%eps",Infer()),list()))
53       tree.rhs=Rhs_tlist(tree.rhs(1),rhs2)
54       tree.name="bdiag"
55       tree.lhs(1).dims=list(A.dims(1),A.dims(1))
56       tree.lhs(1).type=Type(Double,Unknown)
57       tree.lhs(2).dims=list(A.dims(1),A.dims(1))
58       tree.lhs(2).type=Type(Double,Unknown)
59     end
60     return
61   end
62   
63   if B.vtype<>Double then
64     set_infos(gettext("eig() with 2 inputs: consider generalized eigen. Check."),2)
65   end
66   m=A.dims(1)
67   n=A.dims(2)
68   if lhs==1 then
69     al=gettempvar(1)
70     be=gettempvar(2)
71     insert(Equal(list(al,be),Funcall("spec",1,tree.rhs,list(al,be))))
72     tree=Operation("./",list(al,be),tree.lhs)
73     
74     tree.out(1).dims=list(m,1)
75     tree.out(1).type=Type(Double,Unknown)
76   else
77     al=gettempvar(1)
78     be=gettempvar(2)
79
80     insert(Equal(list(al,be,tree.lhs(1)),Funcall("eig",1,tree.rhs,list(al,be,tree.lhs(1)))))
81
82     tree=Operation("./",list(al,be),list(tree.lhs(1)))
83     tree=Funcall("diag",1,list(tree),tree.out)
84
85     tree.lhs(1).dims=list(A.dims(1),A.dims(1))
86     tree.lhs(1).type=Type(Double,Unknown)
87     tree.lhs(2).dims=list(A.dims(1),A.dims(1))
88     tree.lhs(2).type=Type(Double,Unknown)
89   end
90 // eig(A,B,flag)
91 else
92   no_equiv(msprintf(gettext("%s, flag IGNORED."),expression2code(tree)))
93   
94   tree.rhs(3)=null()
95   
96   tree.lhs(1).dims=list(A.dims(1),A.dims(1))
97   tree.lhs(1).type=Type(Double,Unknown)
98   if lhs==2 then
99     tree.lhs(2).dims=list(A.dims(1),A.dims(1))
100     tree.lhs(2).type=Type(Double,Unknown)
101   end
102 end
103
104 endfunction