display of some types and lines function fixed.
[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 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING, which
7 // you should have received as part of this distribution.  The terms
8 // are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10
11 function %r_p(h)
12
13     if exists("with_texmacs")==1 & typeof(with_texmacs)=="function" then
14         texout(h);
15     else
16         //used to display rational fraction with complex coefficients
17         //The real case is hard coded
18         if size(size(h),"*")>2 then
19             //hypermatrix case
20             %hmr_p(h)
21             return
22         end
23
24         [m, n]=size(h);
25         if (m == 0) | (n == 0) then
26             return
27         end
28         del=" "
29         blank=" "
30         if m*n==1 then del=" ",end
31         height=zeros(m,1)  // to store "height" of each row do be displayed
32         width=zeros(1,n) // to store "width" of each column do be displayed
33         T=list() // to store display of each entry of the rational
34         for k=1:n
35             for l=1:m
36                 tlk=r2str(h(l,k))
37                 height(l)=max(size(tlk,1),height(l))
38                 width(k)=max(max(length(tlk)),width(k))
39                 T($+1)=tlk
40             end
41         end
42         ll=lines()
43         k0=0
44
45         //manage column display
46         while %t
47             // find how many columns can be displayed simultaneously
48             last=find(cumsum(width+2)<ll(1)-3);last=last($);
49             if last==[] then last=1,end
50             // form display of these columns
51             txt=[]
52             for l=1:m
53                 txtr=emptystr(height(l),1)
54                 for k=1:last
55                     txtr=txtr+part(blank(ones(height(l),1)),1:2)
56                     tlk=T(l+(k0+k-1)*m)
57                     txtr=txtr+[part(tlk,1:width(k));emptystr(height(l)-size(tlk,1),1)]
58                 end
59                 txt=[txt;txtr]
60             end
61             // add matrix delimiter and columns title and display
62             nt=size(txt,1)
63             txt=part(txt,1:max(length(txt)))
64
65             if k0==0&last==n then
66                 r = del(ones(nt,1))+txt+blank(ones(nt,1))+del(ones(nt,1));
67                 for i=1:size(r,"*")
68                     mprintf("%s\n", r(i))
69                 end
70             else
71                 if last==1 then
72                     leg="column "+string(k0+1)
73                 else
74                     leg="column "+string(k0+1)+" to "+string(k0+last)
75                 end
76
77                 r = [" "; leg; " ";del(ones(nt,1))+txt+blank(ones(nt,1))+del(ones(nt,1))];
78                 for i=1:size(r,"*")
79                     mprintf("%s\n", r(i))
80                 end
81             end
82             width(1:last)=[]
83             k0=last
84             if width==[] then break,end
85         end
86
87     end
88
89 endfunction
90
91 function txt=p2str(p)
92     //form display of a single polynomial with complex coefficients
93     lparen="("
94     rparen=")"
95     blank=" "
96     if type(p)==1 then p=poly(p,"s","c"),end
97     d=degree(p)
98     v=stripblanks(varn(p))
99
100     c=strsubst(string(coeff(p)),"%i","i")
101     // find coefficients with displays as "0" (deleted later)
102     kz=find(c=="0")
103     // find coefficients with displays as "1"
104     k1=find(c=="1");if k1(1)==1 then k1(1)=[],end
105     if k1<>[] then c(k1)=emptystr(1,size(k1,"*")),end
106     // find coefficients with real AND imaginary part (to be parenthezied)
107     kc=find(imag(coeff(p))<>0&real(coeff(p))<>0)
108     w=ones(1,size(kc,"*"))
109     if kc<>[] then c(kc)=lparen(w)+c(kc)+rparen(w),end
110     // add formal variable name
111     c=c+[emptystr(),v(ones(1:d))]
112
113     // form exponents
114     expo1=[" "," ",string(2:d)]
115
116     //delete coeffiecients and exponents corresponding to "0"s
117     c(kz)=[]
118     expo1(kz)=[]
119     if c==[] then
120         c="0"
121         expo1=emptystr()
122     end
123
124     // change coefficients sign display and adjust length of exponents
125     le=0
126     expo=emptystr(c)
127     for kc=1:size(c,"*")
128         if kc>1 then
129             if part(c(kc),1)<>"-" then
130                 c(kc)=" + "+c(kc),
131             else
132                 c(kc)=" - "+part(c(kc),2:length(c(kc)))
133             end
134         end
135         expo(kc)=part(blank,ones(1,length(c(kc))-le))
136         le=length(expo1(kc))
137     end
138     expo=expo+expo1(1:size(c,"*"))
139
140     //Handle long lines
141     ll=lines()
142     nn=size(expo,"*")
143     txt=[]
144     count=0
145     while %t
146         L=cumsum(length(expo))
147         last=find(L<ll(1)-3);last=last($)
148         txt=[txt;
149         part(blank,ones(1,count))+strcat(expo(1:last));
150         strcat(c(1:last))]
151         expo(1:last)=[]
152         c(1:last)=[]
153         if c==[] then break,end
154         count=count+1
155     end
156
157 endfunction
158 function txt=r2str(h)
159     //form display of a single rational with complex coefficients
160     dash="-"
161     blank=" "
162
163     t1=p2str(h("num")) //display of numerator
164     t2=p2str(h("den")) //display of denominator
165
166     //add fraction bar and center
167     l1=max(length(t1))
168     l2=max(length(t2))
169     if l1>l2 then
170         ll1=int((l1-l2)/2)
171         ll2=l1-l2-ll1
172         b=blank(ones(size(t2,"*"),1))
173         txt=[t1;
174         part(dash,ones(1,l1));
175         part(b,ones(1,ll1))+t2+part(b,ones(1,ll2))]
176     elseif l1<l2 then
177         ll1=int((l2-l1)/2)
178         ll2=l2-l1-ll1
179         b=blank(ones(size(t1,"*"),1))
180         txt=[part(b,ones(1,ll1))+t1+part(b,ones(1,ll2));
181         part(dash,ones(1,l2));
182         t2]
183     else
184         txt=[t1;part(dash,ones(1,l1));t2]
185     end
186 endfunction
187
188 function   %hmr_p(h)
189     // hypermatrix display
190     dims=size(h)
191     num=h.num
192     den=h.den
193     nd=size(dims,"*")
194     I=(1:dims(3));
195     for k=4:nd
196         I=[ones(1,dims(k)).*.I;
197         (1:dims(k)).*.ones(1,size(I,2))];
198     end
199     k=1;sz=dims(1)*dims(2)
200     for II=I
201         tit="(:,:,"+strcat(string(II'),",")+")"
202         mprintf("%s\n",tit)
203         hb=rlist(matrix(num(k:k-1+sz),dims(1),dims(2)),matrix(den(k:k-1+sz),dims(1),dims(2)),h.dt)
204         %r_p(hb)
205         k=k+sz
206     end
207 endfunction