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
14function %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);
52            last = last($);
53            if last==[] then last=1,end
54            // form display of these columns
55            txt=[]
56            for l=1:m  // loopon rows
57                txtr=emptystr(height(l),1)
58                for k=1:last  // loop on columns of the current block of columns
59                    txtr = txtr+part(blank(ones(height(l),1)),1:2)
60                    tlk = T(l+(k0+k-1)*m)
61                    iPad = (width(k)-max(length(tlk)))/2;
62                    if iPad >= 1
63                        tlk = part(blank,1:iPad)+tlk;
64                    end
65                    txtr=txtr+[part(tlk,1:width(k));emptystr(height(l)-size(tlk,1),1)]
66                end
67                txt=[txt;txtr];
68                if mode() == 2 && l < m
69                    txt=[txt;emptystr(1)];
70                end
71            end
72            // add matrix delimiter and columns title and display
73            nt = size(txt,1)
74            txt = part(txt,1:max(length(txt)))
75            r = del(ones(nt,1))+txt+blank(ones(nt,1))+del(ones(nt,1));
76            if k0>0 | last<n then  // add columns headers
77                if last==1 then
78                    leg = msprintf("         "+_("column %d"), k0+1)
79                else
80                    leg = msprintf("         "+_("columns %d to %d"), k0+1, k0+last)
81                end
82                r = [" "; leg ; " " ; r];
83            end
84            for i=1:size(r,"*")
85                mprintf("%s\n", r(i))
86            end
87            width(1:last)=[]
88            k0 = k0 + last
89            if width==[] then break,end
90        end
91    end
92endfunction
93
94function txt=p2str(p)
95    //Handle long lines
96    txt=[]
97    pString = string(p);
98    monomLength = cumsum(2+length(strsplit(pString,[" +";" -"])))-2;
99    while %t
100        last = find(monomLength<lines()(1)-3)($)
101        toPrint = part(pString,1:monomLength(last));
102        txt = [txt; toPrint];
103        if degree(p) == -%inf || last == sum(coeff(p)<>0)
104            break
105        end
106        pString = part(pString,monomLength(last)+1:$);
107        monomLength = monomLength - length(toPrint);
108    end
109endfunction
110
111function txt=r2str(h)
112    //form display of a single rational with complex coefficients
113    dash="-";
114    blank=" "
115
116    t1=p2str(h("num")) //display of numerator
117    t2=p2str(h("den")) //display of denominator
118
119    //add fraction bar and center
120    l1=max(length(t1))
121    l2=max(length(t2))
122    if l1>l2 then
123        ll1=int((l1-l2)/2)
124        ll2=l1-l2-ll1
125        b=blank(ones(size(t2,"*"),1))
126        txt=[t1;
127        part(dash,ones(1,l1));
128        part(b,ones(1,ll1))+t2+part(b,ones(1,ll2))]
129    elseif l1<l2 then
130        ll1=int((l2-l1)/2)
131        ll2=l2-l1-ll1
132        b=blank(ones(size(t1,"*"),1))
133        txt=[part(b,ones(1,ll1))+t1+part(b,ones(1,ll2));
134        part(dash,ones(1,l2));
135        t2]
136    else
137        txt=[t1;part(dash,ones(1,l1));t2]
138    end
139endfunction
140
141function   %hmr_p(h)
142    // hypermatrix display
143    dims=size(h)
144    num=h.num
145    den=h.den
146    nd=size(dims,"*")
147    I=(1:dims(3));
148    for k=4:nd
149        I=[ones(1,dims(k)).*.I;
150        (1:dims(k)).*.ones(1,size(I,2))];
151    end
152    k=1;sz=dims(1)*dims(2)
153    for II=I
154        tit="(:,:,"+strcat(string(II'),",")+")"
155        mprintf("%s\n",tit)
156        hb=rlist(matrix(num(k:k-1+sz),dims(1),dims(2)),matrix(den(k:k-1+sz),dims(1),dims(2)),h.dt)
157        %r_p(hb)
158        k=k+sz
159    end
160endfunction
161