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, 2019 - 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 strfun(1) = strsubst(strfun(1), tree.name+"(", name+"(");
91 t(1) = part(t(1),10:length(t(1)))
94 t(1) = "createfun(" + t(1);
97 t = list2exp(a, lmax);
99 t = tlist2exp(a, lmax);
100 case 17 then // cells, struct, mlists
101 if typeof(a)=="st" & length(a)==1
102 t = scalarstruct2exp(a, lmax);
104 t = mlist2exp(a, lmax);
107 t = mlist2exp(user2mlist(a), lmax);
109 t = imp2exp(a, lmax);
111 // execstr('t='+typeof(a)+'2exp(a,lmax)')
112 msg = _("%s: This feature has not been implemented: Variable translation of type %s.\n");
113 error(msprintf(msg, "sci2exp", string(type(a))));
117 s = strcat(msprintf("%d\n",s(:)), ","); // Literal list of sizes
119 if length(t(1)) > (lmax-8)
120 t = ["matrix(.."; t];
122 t(1) = "matrix("+t(1);
124 if length(t($)) > (lmax-length(s)-5)
126 t = [t ; "["+s+"])"];
128 t($) = t($)+", ["+s+"])";
131 t = "matrix(" + t + ", ["+s+"])";
134 if named&and(type(a)<>13) then
139 function t=str2exp(a,lmax)
140 if rhs<2 then lmax=0,end
148 a=strsubst(a,quote,quote+quote)
150 a=strsubst(a,dquote,dquote+dquote)
151 a = strsubst(a, ascii(10), """+ascii(10)+""")
152 a=quote(ones(a))+a+quote(ones(a))
158 y=dquote+part(y,2:length(y))
159 y=part(y,1:length(y)-1)+dquote
160 if y=="''''" then y="emptystr()",end
161 if lmax==0|length(x($))+length(y)<lmax then
176 if i<m then x($)=x($)+";",end
183 if lmax>0&sum(length(t))<lmax then
192 function t=mat2exp(a,lmax)
193 if rhs<2 then lmax=0,end
195 if size(a,"*")==0 then t="[]",return,end
212 if lmax==0|lx<lmax then
218 if lx-l<lmax|k1>length(ind) then,break,end
219 k2=k1-1+max(find(ind(k1:$)<l+lmax))
221 // t=[t;part(x,l+1:ind(k2))]
230 if lmax==0|lx<lmax then
237 if lx-l<lmax|k1>length(ind) then break,end
238 k2=k1-1+max(find(ind(k1:$)<l+lmax))
240 // t=[t;part(x,l+1:ind(k2))+dots]
244 x=strsplit(x,I);x(1:$-1)=x(1:$-1)+dots;
250 if i<m then x=x+";",end
260 if lx-l<lmax|k1>length(ind) then break,end
261 k2=k1-1+max(find(ind(k1:$)<l+lmax))
263 // t=[t;part(x,l+1:ind(k2))+dots]
267 x=strsplit(x,I);x(1:$-1)=x(1:$-1)+dots;x(2:$)=" "+x(2:$);
278 function t=pol2exp(a,lmax)
280 if rhs<2 then lmax = 0, end
287 if or(var==["s" "z"]) then
291 while part(var,lvar)==" " then lvar=lvar-1,end
292 var=part(var,1:lvar);
302 v=a(i,j);d=max(0,degree(v));
304 k0=1;while (k0<d+1)&(v(k0))==0 then k0=k0+1,end
311 if part(s,1)==" " then s=part(s,2:length(s)),end
316 mnm=mnm+"^"+string(k-1),
320 if string(imag(v(k)))<>"0" then s="("+s+")",end
328 if k>k0&part(s,1)<>"-" then s="+"+s,end
330 if lmax==0|length(y($))+length(s) <lmax then
338 if nul then y="0*"+var,end
341 if lmax==0|length(x($))+length(y(1))<lmax then
347 if ny>1 then x($+1:$+ny-1)=y(2:ny),end
349 if length(x($))==0 then
357 if i<m then x($)=x($)+";",end
358 if lmax>0 | t==[] then
364 if lmax>0&sum(length(t))<lmax then
373 function t = glist2exp(listType, l, lmax)
375 if rhs<3 then lmax = 0, end
377 isCell = typeof(l)=="ce";
380 s = strcat(msprintf("%d\n",s(:)),","); // Literal list of sizes
382 if lmax>0 & (length(t) > (lmax-length(s)-4))
383 t = [t + dots; "["+s+"],.. "];
387 // ND-transposition needed due to makecell() special indexing:
400 if or(listType==["list", "mlist", "tlist"]) then
403 L = size(getfield(1,l),"*");
407 sep = ",", if k==1 then sep = "", end
409 if listType ~= "mlist"
414 if ~isdef("lk","local")
417 t1 = sci2exp(lk, lmax)
419 if size(t1,"*")==1&(lmax==0|max(length(t1))+length(t($))<lmax) then
429 function t = list2exp(l, lmax)
430 t = glist2exp("list", l, lmax)
432 function t = tlist2exp(l, lmax)
433 t = glist2exp("tlist", l, lmax)
435 function t = mlist2exp(l, lmax)
436 t = glist2exp("mlist", l, lmax)
439 function t = scalarstruct2exp(l, lmax)
440 if argn(2)<2 then lmax = 0, end
443 fields = fieldnames(l);
444 n = size(fields,"*");
446 if ~lmax | lmax>(12+length(fields(i)))
447 t($) = t($) + """"+fields(i)+""",";
449 t($) = t($) + " " + dots;
450 t = [t; """"+fields(i)+""","];
454 if ~isdef("lk","local")
457 t1 = sci2exp(lk, lmax)
464 if size(t1,"*")==1&(lmax==0|max(length(t1))+length(t($))<lmax) then
465 t($) = t($) + t1 + tmp;
474 function t=log2exp(a,lmax)
476 if rhs<2 then lmax = 0, end
485 a(k)=T(ones(size(k,"*"),1));
494 if lmax==0|length(x($))+length(y)<lmax then
507 if lmax==0|lmax>0&sum(length(t))<lmax then
517 function t=func2exp(a,lmax)
518 if rhs<2 then lmax=0,end
520 [out,in,text]=string(a)
521 if out==[] then out=emptystr(),end
522 text=str2exp(text,lmax)
524 if named then mac=nom,else mac="mac",end
526 t="deff(''["+strcat(out,",")+"]="+mac+"("+strcat(in,",")+")'',"+text+")"
530 text(1:$-1)=text(1:$-1)+semi(ones(nt-1,1))'
531 t=["deff(''["+strcat(out,",")+"]="+mac+"("+strcat(in,",")+")'',"+text(1)
536 function t=imp2exp(a,lmax)
537 if rhs<2 then lmax=0,end
541 t=sci2exp(a(1))+":"+sci2exp(a(3))
543 t=sci2exp(a(1))+":"+sci2exp(a(2))+":"+sci2exp(a(3))
547 function t=sp2exp(a,lmax)
548 if rhs<2 then lmax=0,end
557 if lmax==0|length(t($))+length(v(1))+1<lmax then
564 if lmax==0|length(t($))+length(mn(1))+1<lmax then
575 function t=int2exp(a,lmax)
577 if it>10 then f="uint",else f="int",end
578 f=f+string(8*modulo(it,10))
579 t=mat2exp(double(a),lmax)
585 function t=h2exp(a,lmax) //Only for figure and uicontrol
593 f3="''BackgroundColor'', ";
595 f5="''fontAngle'', ";
598 f8="''fontUnits'', ";
599 f9="''fontWeight'', ";
600 f10="''ForegroundColor'', ";
601 f11="''HorizontalAlignment'', ";
602 f12="''ListboxTop'', ";
605 f15="''Position'', ";
607 f17="''SliderStep'', ";
610 f20="''TooltipString'', ";
613 f23="''VerticalAlignment'', ";
615 f25="''Callback'', ";
616 f26="''Callback_Type'', ";
617 f27="''Handle_Visible'', ";
618 f28="''Userdata'', ";
620 f30="''figure_position'', ";
621 f31="''figure_size'', ";
622 f32="''axes_size'', ";
623 f33="''auto_resize'', ";
624 f34="''viewport'', ";
625 f35="''figure_name'', ";
626 f36="''figure_id'', ";
627 f37="''info_message'', ";
628 f38="''color_map'', ";
629 f40="''pixel_drawing_mode'', ";
630 f41="''anti_aliasing'', ";
631 f42="''immediate_drawing'', ";
632 f43="''background'', ";
633 f44="''rotation_style'', ";
634 f45="''event_handler'', ";
635 f46="''event_handler_enable'', ";
636 f47="''resizefcn'', ";
637 f48="''closerequestfcn'', ";
641 if a.type=="uicontrol"
643 if a.BackgroundColor <> [0.8 0.8 0.8] then
644 if type(a.BackgroundColor)==1 then
645 f3_strg=String(a.Backgroundcolor);
646 f3_strg="["+f3_strg(1)+" "+f3_strg(2)+" "+f3_strg(3)+"]";
649 f3_strg=a.BackgroundColor;
650 x=x+f3+"''"+f3_strg+"''"+", ";
653 if a.Enable <> "on" then x=x+f4+"''"+a.Enable+"''"+", "; end
654 if a.fontAngle <> "normal" then x=x+f5+"''"+a.fontAngle+"''"+", "; end
655 if a.fontName <> "helvetica" then x=x+f6+"''"+a.fontName+"''"+", "; end
656 f7_strg=String(a.fontSize);
657 if a.fontSize <> 10 then x=x+f7+f7_strg+", "; end
658 if a.fontUnits <> "points" then x=x+f8+"''"+a.fontUnits+"''"+", "; end
659 if a.fontWeight <> "normal" then x=x+f9+"''"+a.fontWeight+"''"+", "; end
660 if a.foregroundColor <>[0 0 0] then
661 if type(a.ForegroundColor)==1 then
662 f10_strg=String(a.foregroundColor);
663 f10_strg="["+f10_strg(1)+" "+f10_strg(2)+" "+f10_strg(3)+"]";
664 x=x+f10+f10_strg+" ,";
666 f10_strg=a.ForegroundColor;
667 x=x+f10+"''"+f10_strg+"''"+", ";
670 if a.HorizontalAlignment <> "center" then x=x+f11+"''"+a.horizontalAlignment+"''"+", "; end
671 f12_strg=String(a.ListBoxTop);
672 if a.ListBoxTop <>[] then x=x+f12+f12_strg+", "; end
673 f13_strg=String(a.Max);
674 if a.max<> 1 then x=x+f13+f13_strg+", "; end
675 f14_strg=String(a.Min);
676 if a.min<> 0 then x=x+f14+f14_strg+", "; end
677 if a.Position <>[20 40 40 20] then
678 if type(a.Position)==1 then
679 f15_strg=String(a.Position);
680 f15_strg="["+f15_strg(1)+" "+f15_strg(2)+" "+f15_strg(3)+" "+f15_strg(4)+"]";
681 x=x+f15+f15_strg+", ";
684 x=x+f15+"''"+f15_strg+"''"+", ";
687 x=x+f16+"''"+a.Relief+"''"+", ";
688 f17_strg=String(a.sliderStep); f17_strg="["+f17_strg(1)+" "+f17_strg(2)+"]";
689 if a.sliderStep <> [0.01 0.1] then x=x+f17+f17_strg+", "; end
690 if a.String <>"" then
692 x = x + f18 + "'''' ,";
694 x=x+f18+"''"+a.String+"''"+" ,";
697 if a.Style <> "pushbutton" then x=x+f19+"''"+a.Style+"''"+", "; end
698 if a.TooltipString <> "" then x=x+f20+"''"+a.TooltipString+"''"+", "; end
699 if a.Units <> "pixels" then x=x+f21+"''"+a.Units+"''"+", "; end
701 f22_strg=String(a.value);
702 f22l=length(a.value);
704 f22_strg=strcat(f22_strg, ",");
705 f22_strg="["+f22_strg+"]";
707 x=x+f22+", "+f22_strg+", ";
709 if a.VerticalAlignment <> "middle" then x=x+f23+"''"+a.VerticalAlignment+"''"+", "; end
710 if a.Visible <> "on" then x=x+f24+"''"+a.Visible+"''"+", "; end
711 if a.Callback <>"" then x=x+f25+"''"+a.Callback+"''"+" ,"; end
712 f26_strg=String(a.Callback_Type);
713 if a.Callback_Type <> -1 then x=x+f26+f26_strg+", "; end
714 if a.Handle_Visible <> "on" then x=x+f27+"''"+a.Handle_Visible+"''"+", "; end
715 if a.Userdata <>[] then
716 if type(a.userdata) == 1 then
717 f28_strg=mat2exp(a.userdata,0)
718 elseif type(a.userdata) == 2 then
719 f28_strg=pol2exp(a.userdata,0)
720 elseif type(a.userdata) == 4 then
721 f28_strg=log2exp(a.userdata,0)
722 elseif type(a.userdata) == 5 | type(a.userdata) == 6 | type(a.userdata) == 7 then
723 f28_strg=sp2exp(a.userdata,0)
724 elseif type(a.userdata) == 8 then
725 f28_strg=int2exp(a.userdata,0)
726 elseif type(a.userdata) == 9 then
727 f28_strg=h2exp(a.userdata,0)
728 elseif type(a.userdata) == 10 then
729 f28_strg=str2exp(a.userdata,0)
730 elseif type(a.userdata) == 13 then
732 f28_strg=sci2exp(a.userdata,nom)
734 f28_strg=sci2exp(a.userdata,"%fun")
736 elseif type(a.userdata) == 15 then
737 f28_strg=list2exp(a.userdata);
738 elseif type(a.userdata) == 16 then
739 f28_strg=tlist2exp(a.userdata,0);
740 elseif type(a.userdata) == 17 then
741 f28_strg=mlist2exp(a.userdata,0);
742 elseif type(a.userdata) == 129 then
743 f28_strg=imp2exp(a.userdata,0);
745 x=x+f28+f28_strg+", ";
747 if a.Tag<>"" then x=x+f29+"''"+a.Tag+"''"+", "; end
754 if lt>lmax & lmax<>0 then
758 if lt-l<lmax|k1>length(ind) then break,end
759 k2=k1-1+max(find(ind(k1:$)<l+lmax))
765 t(1:$-1)=t(1:$-1)+dots;
769 elseif a.type == "Figure" then
771 f30_strg=String(a.figure_position);
772 f30_strg="["+f30_strg(1)+" "+f30_strg(2)+"]";
773 if a.figure_position <> [] then x=f30+f30_strg+", "; end
774 f31_strg=String(a.figure_size);
775 f31_strg="["+f31_strg(1)+" "+f31_strg(2)+"]";
776 if a.figure_size <> [] then x=x+f31+f31_strg+", "; end
777 f32_strg=String(a.axes_size);
778 f32_strg="["+f32_strg(1)+" "+f32_strg(2)+"]";
779 if a.axes_size <> [] then x=x+f32+f32_strg+", "; end
780 if a.auto_resize<>"on" then x=x+f33+"''"+a.auto_resize+"''"+", "; end
781 f34_strg=String(a.viewport);
782 f34_strg="["+f34_strg(1)+" "+f34_strg(2)+"]";
783 if a.viewport <>[0 0] then x=x+f34+f34_strg+", "; end
784 if a.figure_name<>"Figure n°%d" then x=x+f35+"''"+a.figure_name+"''"+", ";
786 f36_strg=String(a.figure_id);
787 x=x+f36+f36_strg+", ";
788 if a.info_message<>"" then x=x+f37+"''"+a.info_message+"''"+", "; end
789 if a.pixel_drawing_mode <> "copy" then x=x+f40+"''"+a.pixel_drawing_mode+"''"+", "; end
790 if a.anti_aliasing <> "off" then x=x+f41+"''"+a.anti_aliasing+"''"+", "; end
791 if a.immediate_drawing <>"on" then x=x+f42+"''"+a.immediate_drawing+"''"+", "; end
792 f43_strg=String(f43);
793 if a.background <> 33 then x=x+f43+f43_strg+", "; end
794 if a.visible <> "on" then x=x+f24+"''"+a.visible+"''"+", "; end
795 if a.rotation_style <> "unary" then x=x+f44+"''"+a.rotation_style+"''"+", "; end
796 if a.event_handler <> "" then x=x+f45+"''"+a.event_handler+"''"+", "; end
797 if a.event_handler_enable <> "off" then x=x+f46+"''"+a.event_handler_enable+"''"+", "; end
798 if a.userdata <> [] then
799 if type(a.userdata) == 1 then
800 f47_strg=mat2exp(a.userdata,0)
801 elseif type(a.userdata) == 2 then
802 f47_strg=pol2exp(a.userdata,0)
803 elseif type(a.userdata) == 4 then
804 f47_strg=log2exp(a.userdata,0)
805 elseif type(a.userdata) == 5 | type(a.userdata) == 6 | type(a.userdata) == 7 then
806 f47_strg=sp2exp(a.userdata,0)
807 elseif type(a.userdata) == 8 then
808 f47_strg=int2exp(a.userdata,0)
809 elseif type(a.userdata) == 9 then
810 f47_strg=h2exp(a.userdata,0)
811 elseif type(a.userdata) == 10 then
812 f47_strg=str2exp(a.userdata,0)
813 elseif type(a.userdata) == 13 then
815 f47_strg=sci2exp(a.userdata,nom)
817 f47_strg=sci2exp(a.userdata,"%fun")
819 elseif type(a.userdata) == 15 then
820 f47_strg=list2exp(a.userdata);
821 elseif type(a.userdata) == 16 then
822 f47_strg=tlist2exp(a.userdata,0)
823 elseif type(a.userdata) == 17 then
824 f47_strg=mlist2exp(a.userdata,0)
825 elseif type(a.userdata) == 129 then
826 f47_strg=imp2exp(a.userdata,0)
828 x=x+f28+f47_strg+", ";
830 if a.resizefcn <> "" then x=x+f47+"''"+a.resizefcn+"''"+", "; end
831 if a.closerequestfcn <> "" then x=x+f48+"''"+a.closerequestfcn+"''"+", "; end
832 if a.tag <> "" then x=x+f27+"''"+a.tag+"''"+", "; end
839 if lt>lmax & lmax<>0 then
843 if lt-l<lmax|k1>length(ind) then break,end
844 k2=k1-1+max(find(ind(k1:$)<l+lmax))
850 t(1:$-1)=t(1:$-1)+dots;
853 msg = _("%s: This feature has not been implemented: Variable translation of type %s.\n")
854 error(msprintf(msg,"sci2exp",string(a.type)));
859 function ml = user2mlist(u)
863 for k = 1:size(fn,"*")