1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) INRIA -
3 // Copyright (C) 2012 - 2016 - Scilab Enterprises
4 // Copyright (C) 2016, 2017 - Samuel GOUGEON
6 // This file is hereby licensed under the terms of the GNU GPL v2.0,
7 // pursuant to article 5.3.4 of the CeCILL v.2.1.
8 // This file was originally licensed under the terms of the CeCILL v2.1,
9 // and continues to be available under such terms.
10 // For more information, see the COPYING file which you should have received
11 // along with this program.
13 function t=sci2exp(a,nom,lmax)
14 // sci2exp - convert a variable to an expression
16 // t=sci2exp(a [,nam] [,lmax])
18 // a : matrix of scalar or polynomials
19 // nam: character string
20 // t : vector of string, contains the expression definition
21 // lmax : maximum line lengh (0 for no line length control)
27 deff("x=String(a)",["x=string(a)"
28 "x=strsubst(x,''Nan'',''%nan'')"
29 "x=strsubst(x,''Inf'',''%inf'')"
34 $ // import global variable $ in sci2exp context for efficiency
53 // For an hypermatrix, we concatenate all components in a single row:
54 hyperMat = or(type(a)==[1 2 4 8 10]) && ndims(a) > 2;
83 strfun=tree2code(tree);
88 idx=strindex(strfun(1), "=");
89 idx2=strindex(part(strfun(1), idx:$), "(") + idx - 1;
90 str=part(strfun(1), 1:idx) + " "+ name + part(strfun(1), idx2:length(strfun(1)));
94 t(1) = part(t(1),10:length(t(1)))
97 t(1) = "createfun(" + t(1);
100 t = list2exp(a, lmax);
102 t = tlist2exp(a, lmax);
103 case 17 then // cells, struct, mlists
104 if typeof(a)=="st" & length(a)==1
105 t = scalarstruct2exp(a, lmax);
107 t = mlist2exp(a, lmax);
110 t = mlist2exp(user2mlist(a), lmax);
112 t = imp2exp(a, lmax);
114 // execstr('t='+typeof(a)+'2exp(a,lmax)')
115 msg = _("%s: This feature has not been implemented: Variable translation of type %s.\n");
116 error(msprintf(msg, "sci2exp", string(type(a))));
120 s = strcat(msprintf("%d\n",s(:)), ","); // Literal list of sizes
122 if length(t(1)) > (lmax-8)
123 t = ["matrix(.."; t];
125 t(1) = "matrix("+t(1);
127 if length(t($)) > (lmax-length(s)-5)
129 t = [t ; "["+s+"])"];
131 t($) = t($)+", ["+s+"])";
134 t = "matrix(" + t + ", ["+s+"])";
137 if named&and(type(a)<>13) then
142 function t=str2exp(a,lmax)
143 if rhs<2 then lmax=0,end
151 a=strsubst(a,quote,quote+quote)
153 a=strsubst(a,dquote,dquote+dquote)
154 a = strsubst(a, ascii(10), """+ascii(10)+""")
155 a=quote(ones(a))+a+quote(ones(a))
161 y=dquote+part(y,2:length(y))
162 y=part(y,1:length(y)-1)+dquote
163 if y=="''''" then y="emptystr()",end
164 if lmax==0|length(x($))+length(y)<lmax then
179 if i<m then x($)=x($)+";",end
186 if lmax>0&sum(length(t))<lmax then
195 function t=mat2exp(a,lmax)
196 if rhs<2 then lmax=0,end
198 if size(a,"*")==0 then t="[]",return,end
215 if lmax==0|lx<lmax then
221 if lx-l<lmax|k1>length(ind) then,break,end
222 k2=k1-1+max(find(ind(k1:$)<l+lmax))
224 // t=[t;part(x,l+1:ind(k2))]
233 if lmax==0|lx<lmax then
240 if lx-l<lmax|k1>length(ind) then break,end
241 k2=k1-1+max(find(ind(k1:$)<l+lmax))
243 // t=[t;part(x,l+1:ind(k2))+dots]
247 x=strsplit(x,I);x(1:$-1)=x(1:$-1)+dots;
253 if i<m then x=x+";",end
263 if lx-l<lmax|k1>length(ind) then break,end
264 k2=k1-1+max(find(ind(k1:$)<l+lmax))
266 // t=[t;part(x,l+1:ind(k2))+dots]
270 x=strsplit(x,I);x(1:$-1)=x(1:$-1)+dots;x(2:$)=" "+x(2:$);
281 function t=pol2exp(a,lmax)
283 if rhs<2 then lmax = 0, end
290 if or(var==["s" "z"]) then
294 while part(var,lvar)==" " then lvar=lvar-1,end
295 var=part(var,1:lvar);
305 v=a(i,j);d=max(0,degree(v));
307 k0=1;while (k0<d+1)&(v(k0))==0 then k0=k0+1,end
314 if part(s,1)==" " then s=part(s,2:length(s)),end
319 mnm=mnm+"^"+string(k-1),
323 if string(imag(v(k)))<>"0" then s="("+s+")",end
331 if k>k0&part(s,1)<>"-" then s="+"+s,end
333 if lmax==0|length(y($))+length(s) <lmax then
341 if nul then y="0*"+var,end
344 if lmax==0|length(x($))+length(y(1))<lmax then
350 if ny>1 then x($+1:$+ny-1)=y(2:ny),end
352 if length(x($))==0 then
360 if i<m then x($)=x($)+";",end
361 if lmax>0 | t==[] then
367 if lmax>0&sum(length(t))<lmax then
376 function t = glist2exp(listType, l, lmax)
378 if rhs<3 then lmax = 0, end
380 isCell = typeof(l)=="ce";
383 s = strcat(msprintf("%d\n",s(:)),","); // Literal list of sizes
385 if lmax>0 & (length(t) > (lmax-length(s)-4))
386 t = [t + dots; "["+s+"],.. "];
390 // ND-transposition needed due to makecell() special indexing:
403 if or(listType==["list", "mlist", "tlist"]) then
406 L = size(getfield(1,l),"*");
410 sep = ",", if k==1 then sep = "", end
412 if listType ~= "mlist"
417 if ~isdef("lk","local")
420 t1 = sci2exp(lk, lmax)
422 if size(t1,"*")==1&(lmax==0|max(length(t1))+length(t($))<lmax) then
432 function t = list2exp(l, lmax)
433 t = glist2exp("list", l, lmax)
435 function t = tlist2exp(l, lmax)
436 t = glist2exp("tlist", l, lmax)
438 function t = mlist2exp(l, lmax)
439 t = glist2exp("mlist", l, lmax)
442 function t = scalarstruct2exp(l, lmax)
443 if argn(2)<2 then lmax = 0, end
446 fields = fieldnames(l);
447 n = size(fields,"*");
449 if ~lmax | lmax>(12+length(fields(i)))
450 t($) = t($) + """"+fields(i)+""",";
452 t($) = t($) + " " + dots;
453 t = [t; """"+fields(i)+""","];
457 if ~isdef("lk","local")
460 t1 = sci2exp(lk, lmax)
467 if size(t1,"*")==1&(lmax==0|max(length(t1))+length(t($))<lmax) then
468 t($) = t($) + t1 + tmp;
477 function t=log2exp(a,lmax)
479 if rhs<2 then lmax = 0, end
488 a(k)=T(ones(size(k,"*"),1));
497 if lmax==0|length(x($))+length(y)<lmax then
510 if lmax==0|lmax>0&sum(length(t))<lmax then
520 function t=func2exp(a,lmax)
521 if rhs<2 then lmax=0,end
523 [out,in,text]=string(a)
524 if out==[] then out=emptystr(),end
525 text=str2exp(text,lmax)
527 if named then mac=nom,else mac="mac",end
529 t="deff(''["+strcat(out,",")+"]="+mac+"("+strcat(in,",")+")'',"+text+")"
533 text(1:$-1)=text(1:$-1)+semi(ones(nt-1,1))'
534 t=["deff(''["+strcat(out,",")+"]="+mac+"("+strcat(in,",")+")'',"+text(1)
539 function t=imp2exp(a,lmax)
540 if rhs<2 then lmax=0,end
544 t=sci2exp(a(1))+":"+sci2exp(a(3))
546 t=sci2exp(a(1))+":"+sci2exp(a(2))+":"+sci2exp(a(3))
550 function t=sp2exp(a,lmax)
551 if rhs<2 then lmax=0,end
560 if lmax==0|length(t($))+length(v(1))+1<lmax then
567 if lmax==0|length(t($))+length(mn(1))+1<lmax then
578 function t=int2exp(a,lmax)
580 if it>10 then f="uint",else f="int",end
581 f=f+string(8*modulo(it,10))
582 t=mat2exp(double(a),lmax)
588 function t=h2exp(a,lmax) //Only for figure and uicontrol
596 f3="''BackgroundColor'', ";
598 f5="''fontAngle'', ";
601 f8="''fontUnits'', ";
602 f9="''fontWeight'', ";
603 f10="''ForegroundColor'', ";
604 f11="''HorizontalAlignment'', ";
605 f12="''ListboxTop'', ";
608 f15="''Position'', ";
610 f17="''SliderStep'', ";
613 f20="''TooltipString'', ";
616 f23="''VerticalAlignment'', ";
618 f25="''Callback'', ";
619 f26="''Callback_Type'', ";
620 f27="''Handle_Visible'', ";
621 f28="''Userdata'', ";
623 f30="''figure_position'', ";
624 f31="''figure_size'', ";
625 f32="''axes_size'', ";
626 f33="''auto_resize'', ";
627 f34="''viewport'', ";
628 f35="''figure_name'', ";
629 f36="''figure_id'', ";
630 f37="''info_message'', ";
631 f38="''color_map'', ";
632 f40="''pixel_drawing_mode'', ";
633 f41="''anti_aliasing'', ";
634 f42="''immediate_drawing'', ";
635 f43="''background'', ";
636 f44="''rotation_style'', ";
637 f45="''event_handler'', ";
638 f46="''event_handler_enable'', ";
639 f47="''resizefcn'', ";
640 f48="''closerequestfcn'', ";
644 if a.type=="uicontrol"
646 if a.BackgroundColor <> [0.8 0.8 0.8] then
647 if type(a.BackgroundColor)==1 then
648 f3_strg=String(a.Backgroundcolor);
649 f3_strg="["+f3_strg(1)+" "+f3_strg(2)+" "+f3_strg(3)+"]";
652 f3_strg=a.BackgroundColor;
653 x=x+f3+"''"+f3_strg+"''"+", ";
656 if a.Enable <> "on" then x=x+f4+"''"+a.Enable+"''"+", "; end
657 if a.fontAngle <> "normal" then x=x+f5+"''"+a.fontAngle+"''"+", "; end
658 if a.fontName <> "helvetica" then x=x+f6+"''"+a.fontName+"''"+", "; end
659 f7_strg=String(a.fontSize);
660 if a.fontSize <> 10 then x=x+f7+f7_strg+", "; end
661 if a.fontUnits <> "points" then x=x+f8+"''"+a.fontUnits+"''"+", "; end
662 if a.fontWeight <> "normal" then x=x+f9+"''"+a.fontWeight+"''"+", "; end
663 if a.foregroundColor <>[0 0 0] then
664 if type(a.ForegroundColor)==1 then
665 f10_strg=String(a.foregroundColor);
666 f10_strg="["+f10_strg(1)+" "+f10_strg(2)+" "+f10_strg(3)+"]";
667 x=x+f10+f10_strg+" ,";
669 f10_strg=a.ForegroundColor;
670 x=x+f10+"''"+f10_strg+"''"+", ";
673 if a.HorizontalAlignment <> "center" then x=x+f11+"''"+a.horizontalAlignment+"''"+", "; end
674 f12_strg=String(a.ListBoxTop);
675 if a.ListBoxTop <>[] then x=x+f12+f12_strg+", "; end
676 f13_strg=String(a.Max);
677 if a.max<> 1 then x=x+f13+f13_strg+", "; end
678 f14_strg=String(a.Min);
679 if a.min<> 0 then x=x+f14+f14_strg+", "; end
680 if a.Position <>[20 40 40 20] then
681 if type(a.Position)==1 then
682 f15_strg=String(a.Position);
683 f15_strg="["+f15_strg(1)+" "+f15_strg(2)+" "+f15_strg(3)+" "+f15_strg(4)+"]";
684 x=x+f15+f15_strg+", ";
687 x=x+f15+"''"+f15_strg+"''"+", ";
690 x=x+f16+"''"+a.Relief+"''"+", ";
691 f17_strg=String(a.sliderStep); f17_strg="["+f17_strg(1)+" "+f17_strg(2)+"]";
692 if a.sliderStep <> [0.01 0.1] then x=x+f17+f17_strg+", "; end
693 if a.String <>"" then
695 x = x + f18 + "'''' ,";
697 x=x+f18+"''"+a.String+"''"+" ,";
700 if a.Style <> "pushbutton" then x=x+f19+"''"+a.Style+"''"+", "; end
701 if a.TooltipString <> "" then x=x+f20+"''"+a.TooltipString+"''"+", "; end
702 if a.Units <> "pixels" then x=x+f21+"''"+a.Units+"''"+", "; end
704 f22_strg=String(a.value);
705 f22l=length(a.value);
707 f22_strg=strcat(f22_strg, ",");
708 f22_strg="["+f22_strg+"]";
710 x=x+f22+", "+f22_strg+", ";
712 if a.VerticalAlignment <> "middle" then x=x+f23+"''"+a.VerticalAlignment+"''"+", "; end
713 if a.Visible <> "on" then x=x+f24+"''"+a.Visible+"''"+", "; end
714 if a.Callback <>"" then x=x+f25+"''"+a.Callback+"''"+" ,"; end
715 f26_strg=String(a.Callback_Type);
716 if a.Callback_Type <> -1 then x=x+f26+f26_strg+", "; end
717 if a.Handle_Visible <> "on" then x=x+f27+"''"+a.Handle_Visible+"''"+", "; end
718 if a.Userdata <>[] then
719 if type(a.userdata) == 1 then
720 f28_strg=mat2exp(a.userdata,0)
721 elseif type(a.userdata) == 2 then
722 f28_strg=pol2exp(a.userdata,0)
723 elseif type(a.userdata) == 4 then
724 f28_strg=log2exp(a.userdata,0)
725 elseif type(a.userdata) == 5 | type(a.userdata) == 6 | type(a.userdata) == 7 then
726 f28_strg=sp2exp(a.userdata,0)
727 elseif type(a.userdata) == 8 then
728 f28_strg=int2exp(a.userdata,0)
729 elseif type(a.userdata) == 9 then
730 f28_strg=h2exp(a.userdata,0)
731 elseif type(a.userdata) == 10 then
732 f28_strg=str2exp(a.userdata,0)
733 elseif type(a.userdata) == 13 then
735 f28_strg=sci2exp(a.userdata,nom)
737 f28_strg=sci2exp(a.userdata,"%fun")
739 elseif type(a.userdata) == 15 then
740 f28_strg=list2exp(a.userdata);
741 elseif type(a.userdata) == 16 then
742 f28_strg=tlist2exp(a.userdata,0);
743 elseif type(a.userdata) == 17 then
744 f28_strg=mlist2exp(a.userdata,0);
745 elseif type(a.userdata) == 129 then
746 f28_strg=imp2exp(a.userdata,0);
748 x=x+f28+f28_strg+", ";
750 if a.Tag<>"" then x=x+f29+"''"+a.Tag+"''"+", "; end
757 if lt>lmax & lmax<>0 then
761 if lt-l<lmax|k1>length(ind) then break,end
762 k2=k1-1+max(find(ind(k1:$)<l+lmax))
768 t(1:$-1)=t(1:$-1)+dots;
772 elseif a.type == "Figure" then
774 f30_strg=String(a.figure_position);
775 f30_strg="["+f30_strg(1)+" "+f30_strg(2)+"]";
776 if a.figure_position <> [] then x=f30+f30_strg+", "; end
777 f31_strg=String(a.figure_size);
778 f31_strg="["+f31_strg(1)+" "+f31_strg(2)+"]";
779 if a.figure_size <> [] then x=x+f31+f31_strg+", "; end
780 f32_strg=String(a.axes_size);
781 f32_strg="["+f32_strg(1)+" "+f32_strg(2)+"]";
782 if a.axes_size <> [] then x=x+f32+f32_strg+", "; end
783 if a.auto_resize<>"on" then x=x+f33+"''"+a.auto_resize+"''"+", "; end
784 f34_strg=String(a.viewport);
785 f34_strg="["+f34_strg(1)+" "+f34_strg(2)+"]";
786 if a.viewport <>[0 0] then x=x+f34+f34_strg+", "; end
787 if a.figure_name<>"Figure n°%d" then x=x+f35+"''"+a.figure_name+"''"+", ";
789 f36_strg=String(a.figure_id);
790 x=x+f36+f36_strg+", ";
791 if a.info_message<>"" then x=x+f37+"''"+a.info_message+"''"+", "; end
792 if a.pixel_drawing_mode <> "copy" then x=x+f40+"''"+a.pixel_drawing_mode+"''"+", "; end
793 if a.anti_aliasing <> "off" then x=x+f41+"''"+a.anti_aliasing+"''"+", "; end
794 if a.immediate_drawing <>"on" then x=x+f42+"''"+a.immediate_drawing+"''"+", "; end
795 f43_strg=String(f43);
796 if a.background <> 33 then x=x+f43+f43_strg+", "; end
797 if a.visible <> "on" then x=x+f24+"''"+a.visible+"''"+", "; end
798 if a.rotation_style <> "unary" then x=x+f44+"''"+a.rotation_style+"''"+", "; end
799 if a.event_handler <> "" then x=x+f45+"''"+a.event_handler+"''"+", "; end
800 if a.event_handler_enable <> "off" then x=x+f46+"''"+a.event_handler_enable+"''"+", "; end
801 if a.userdata <> [] then
802 if type(a.userdata) == 1 then
803 f47_strg=mat2exp(a.userdata,0)
804 elseif type(a.userdata) == 2 then
805 f47_strg=pol2exp(a.userdata,0)
806 elseif type(a.userdata) == 4 then
807 f47_strg=log2exp(a.userdata,0)
808 elseif type(a.userdata) == 5 | type(a.userdata) == 6 | type(a.userdata) == 7 then
809 f47_strg=sp2exp(a.userdata,0)
810 elseif type(a.userdata) == 8 then
811 f47_strg=int2exp(a.userdata,0)
812 elseif type(a.userdata) == 9 then
813 f47_strg=h2exp(a.userdata,0)
814 elseif type(a.userdata) == 10 then
815 f47_strg=str2exp(a.userdata,0)
816 elseif type(a.userdata) == 13 then
818 f47_strg=sci2exp(a.userdata,nom)
820 f47_strg=sci2exp(a.userdata,"%fun")
822 elseif type(a.userdata) == 15 then
823 f47_strg=list2exp(a.userdata);
824 elseif type(a.userdata) == 16 then
825 f47_strg=tlist2exp(a.userdata,0)
826 elseif type(a.userdata) == 17 then
827 f47_strg=mlist2exp(a.userdata,0)
828 elseif type(a.userdata) == 129 then
829 f47_strg=imp2exp(a.userdata,0)
831 x=x+f28+f47_strg+", ";
833 if a.resizefcn <> "" then x=x+f47+"''"+a.resizefcn+"''"+", "; end
834 if a.closerequestfcn <> "" then x=x+f48+"''"+a.closerequestfcn+"''"+", "; end
835 if a.tag <> "" then x=x+f27+"''"+a.tag+"''"+", "; end
842 if lt>lmax & lmax<>0 then
846 if lt-l<lmax|k1>length(ind) then break,end
847 k2=k1-1+max(find(ind(k1:$)<l+lmax))
853 t(1:$-1)=t(1:$-1)+dots;
856 msg = _("%s: This feature has not been implemented: Variable translation of type %s.\n")
857 error(msprintf(msg,"sci2exp",string(a.type)));
862 function ml = user2mlist(u)
866 for k = 1:size(fn,"*")