[ast] improve display of (complex) polymomials and rationals
[scilab.git] / scilab / modules / overloading / macros / %r_p.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) INRIA
3 // Copyright (C) DIGITEO - 2012 - Allan CORNET
4 //
5 // Copyright (C) 2012 - 2016 - Scilab Enterprises
6 //
7 // This file is hereby licensed under the terms of the GNU GPL v2.0,
8 // pursuant to article 5.3.4 of the CeCILL v.2.1.
9 // This file was originally licensed under the terms of the CeCILL v2.1,
10 // and continues to be available under such terms.
11 // For more information, see the COPYING file which you should have received
12 // along with this program.
13
14 function %r_p(h)
15
16     if exists("with_texmacs")==1 & typeof(with_texmacs)=="function" then
17         texout(h);
18     else
19         //used to display rational fraction with complex coefficients
20         //The real case is hard coded
21         if size(size(h),"*")>2 then
22             //hypermatrix case
23             %hmr_p(h)
24             return
25         end
26
27         [m, n]=size(h);
28         if (m == 0) | (n == 0) then
29             return
30         end
31         del=" "
32         blank=" "
33         if m*n==1 then del=" ",end
34         height=zeros(m,1)  // to store "height" of each row do be displayed
35         width=zeros(1,n) // to store "width" of each column do be displayed
36         T=list() // to store display of each entry of the rational
37         for k=1:n
38             for l=1:m
39                 tlk=r2str(h(l,k))
40                 height(l)=max(size(tlk,1),height(l))
41                 width(k)=max(max(length(tlk)),width(k))
42                 T($+1)=tlk
43             end
44         end
45         ll=lines()
46         k0=0
47
48         //manage column display
49         while %t
50             // find how many columns can be displayed simultaneously
51             last=find(cumsum(width+2)<ll(1)-3);last=last($);
52             if last==[] then last=1,end
53             // form display of these columns
54             txt=[]
55             for l=1:m
56                 txtr=emptystr(height(l),1)
57                 for k=1:last
58                     txtr=txtr+part(blank(ones(height(l),1)),1:2)
59                     tlk=T(l+(k0+k-1)*m)
60                     iPad = (width(k)-max(length(tlk)))/2;
61                     if iPad>=1
62                         tlk=part(blank,1:iPad)+tlk;
63                     end
64                     txtr=txtr+[part(tlk,1:width(k));emptystr(height(l)-size(tlk,1),1)]
65                 end
66                 txt=[txt;ascii(13);txtr]
67             end
68             // add matrix delimiter and columns title and display
69             nt=size(txt,1)
70             txt=part(txt,1:max(length(txt)))
71
72             if k0==0&last==n then
73                 r = del(ones(nt,1))+txt+blank(ones(nt,1))+del(ones(nt,1));
74                 for i=1:size(r,"*")
75                     mprintf("%s\n", r(i))
76                 end
77             else
78                 if last==1 then
79                     leg="column "+string(k0+1)
80                 else
81                     leg="column "+string(k0+1)+" to "+string(k0+last)
82                 end
83
84                 r = [" "; leg; " ";del(ones(nt,1))+txt+blank(ones(nt,1))+del(ones(nt,1))];
85                 for i=1:size(r,"*")
86                     mprintf("%s\n", r(i))
87                 end
88             end
89             width(1:last)=[]
90             k0=last
91             if width==[] then break,end
92         end
93
94     end
95
96 endfunction
97
98 function txt=r2str(h)
99     //form display of a single rational with complex coefficients
100     dash=ascii([226 148 128]);
101     blank=" "
102
103     t1=string(h("num")) //display of numerator
104     t2=string(h("den")) //display of denominator
105
106     //add fraction bar and center
107     l1=max(length(t1))
108     l2=max(length(t2))
109     if l1>l2 then
110         ll1=int((l1-l2)/2)
111         ll2=l1-l2-ll1
112         b=blank(ones(size(t2,"*"),1))
113         txt=[t1;
114         part(dash,ones(1,l1));
115         part(b,ones(1,ll1))+t2+part(b,ones(1,ll2))]
116     elseif l1<l2 then
117         ll1=int((l2-l1)/2)
118         ll2=l2-l1-ll1
119         b=blank(ones(size(t1,"*"),1))
120         txt=[part(b,ones(1,ll1))+t1+part(b,ones(1,ll2));
121         part(dash,ones(1,l2));
122         t2]
123     else
124         txt=[t1;part(dash,ones(1,l1));t2]
125     end
126 endfunction
127
128 function   %hmr_p(h)
129     // hypermatrix display
130     dims=size(h)
131     num=h.num
132     den=h.den
133     nd=size(dims,"*")
134     I=(1:dims(3));
135     for k=4:nd
136         I=[ones(1,dims(k)).*.I;
137         (1:dims(k)).*.ones(1,size(I,2))];
138     end
139     k=1;sz=dims(1)*dims(2)
140     for II=I
141         tit="(:,:,"+strcat(string(II'),",")+")"
142         mprintf("%s\n",tit)
143         hb=rlist(matrix(num(k:k-1+sz),dims(1),dims(2)),matrix(den(k:k-1+sz),dims(1),dims(2)),h.dt)
144         %r_p(hb)
145         k=k+sz
146     end
147 endfunction