/////////////////////////////////////////////////////////////////////////////// version="version mathml.lib 4.1.2.0 Feb_2019 "; // $Id$ category="Miscellaneous"; info=" LIBRARY: mathml.lib printing mathml code AUTHORS: J. Boehm, boehm @ mathematik.uni-kl.de M. Mueller, mkmuelle @ mathematik.uni-kl.de H. Rombach, HRombach @ gmx.de M. Stein, maxstein77 @ web.de OVERVIEW: mathml conversion KEYWORDS: mathml PROCEDURES: mathml(def) general procedure to generate a mathml code from a Singular object viewMathml(string) view mathml code "; LIB "matrix.lib"; /********************************************************* ** ** ** TOOLs ** ** ** *********************************************************/ static proc mathmlStringReplace(string s,string dummy,string origin)"USAGE: texStringReplace(s); where s is a string ASSUME: s is a string RETURN: s including origin instead of dummy THEORY: if s contains dummy, these parts will be replaced by origin "{ int k,L; string S; S = s; while(find(S,dummy)>0){ k = find(S,dummy); L = size(dummy); if(k+L<=size(S)){ S = S[1,k-1] + origin + S[k+L..size(S)]; } else{S = S[1,k-1] + origin;} } return(S); } proc mathmlRingType(){ string S = charstr(basering); int i = find(S,","); int n; if(i>0){S = S[1,i-1];} if(S=="real" || S =="complex"){return(0)} else{ if (npars(basering)>0) { n=ringlist(basering)[1][1]; if (n!=0 && prime(n)!=n) {return(2);} else {return(1);}} else {return(1);} } } /********************************************************* ** ** ** POLY ** ** ** *********************************************************/ proc mathmlLeadCoef(string s) { string t; int i=1; int u=0; if (s=="1"){return(t);} if (s=="-1"){return("-");} while (i "; while (i<=size(s)) { if (s[i]=="/") {t=t+" ";} else {t=t+s[i];} i=i+1; } t=t+" "; } if (u==0) { if (s[1]=="-") {t=t+"-";s=s[2,size(s)-1];} t=t+""+s+"";} return(t); } proc mathmlVariablesEncoding(string s) { string S; int p; if(s[size(s)]==")"){ p = find(s,"("); S = S + ""+s[1,p-1] + "" + s[p+1,size(s)-p-1]+ ""; } else{ if (size(s)>1) { S = S + ""+s[1] + "" + s[2,size(s)-1]+ "";} else {S=""+s+"";} } return(S); } proc mathmlMonomial(poly a) { string g; intvec d=leadexp(a); list e; for(int q=1;q<=size(variables(a));q++) { e[q]=variables(a)[q]; } int k=1; for(int i=1;i<=size(d);i++) { if (d[i]>0) { g=g+""+mathmlVariablesEncoding(string(e[k]))+""; if (d[i]>1) {g=g+string(d[i]);} g=g+""; k=k+1;} } return(g); } proc mathmlHilf1(poly a,def r) { poly d=a; list l1=ringlist(r); list l12=ringlist(r); for(int i=1;i<=size(l12[1][2]);i++) { l12[2][i]=l12[1][2][i]+"1"; l12[1][2][i]=l12[1][2][i]+"0"; } def w=ring(l12); setring(w); poly f=fetch(r,d); list l12=ringlist(basering); for (i=1;i<=size(l12[1][2]);i++) { f=subst(f,par(i),var(i)); } while (size(l12[2])>size(l12[1][2])) { l12[2]=delete(l12[2],size(l12[1][2])+1); } for (i=1;i<=size(l12[2]);i++) { l12[2][i]=l12[2][i][1,size(l12[2][i])-1]; } l12[1]=l12[1][1]; def w2=ring(l12); setring(w2); keepring(w2); poly f=fetch(w,f); return(f); } proc mathmlHilf(poly a,def r) { string g; def f=mathmlHilf1(a,r); g=g+mathmlPoly(f,basering); return(g); } proc SignumLeadcoef(poly a) { int n=0; def f=mathmlHilf1(a,basering); if (string(leadcoef(f))[1,1]=="-") { n=1; } return(n); } proc mathmlPolyGAL(poly p){ string S; for(int i = 1;i+";} } S = S+ mathmlTermGAL(p[i]); return(S); } proc mathmlTermGAL(poly p){ int b = 0; int B = 0; string S,S1,C,Pname; Pname = ringlist(basering)[1][2][1]; C = string(leadcoef(p)); C = mathmlStringReplace(C,Pname,""); C = mathmlStringReplace(C,"^",""); if (p==-1 || p==1 || p==0) {return(""+string(p)+"");} else { if (C=="-1") {S=S+"-";} else { if (C!="1") {S =S+ ""+Pname+""+C +"";} } S=S+mathmlMonomial(leadmonom(p)); return(S); } } proc mathmlPolyR(poly a) { string S; def origin = basering; if(npars(origin) == 0){ list l = ringlist(basering); l[1][3] = "parTMP"; def ringCOMPLEX = ring(l); setring ringCOMPLEX; poly a = fetch(origin,a); } string S1; string S2; poly d; string s; for (int i=1;i<=size(a);i++) { S2=""; d=a[i]; s=string(leadcoef(d)); if (s[1]=="-" || s[2]=="-") {S=S+"-";d=-1*d;} else {S=S+"+";} S1=mathmlMonomial(leadmonom(d)); if (leadcoef(d)==1) { if (S1=="") {S=S+"1";} } else { s=string(leadcoef(d)); S2=mathmlStringReplace(s,"*1)",")"); S2=mathmlStringReplace(S2,"*",""); if (npars(basering)==1) { if (find(S2,string(par(1)))>0) { S2=mathmlStringReplace(S2,string(par(1)),""); if (size(S2)>0) {S2=S2[1,size(S2)-1]+string(par(1))+")";} else {S2=string(par(1));} } } } S=S+S2+S1; } if (S[1,10]=="+") {S=S[11..size(S)];} return(S); } proc mathmlPolyN(poly a,def r){ string g; number d=leadcoef(a); list l12=ringlist(r); int n=npars(basering); if (n>0) { if (SignumLeadcoef(numerator(d))) { g=g+"-"; d=-d; } if (d!=1) { if (denominator(d)==1) { def f=mathmlHilf1(d,r); if (leadexp(f)==0) {setring r; g=g+mathmlHilf(d,r);} else {setring r; g=g+"("+mathmlHilf(d,r)+")";} } else {g=g+""+mathmlHilf(numerator(d),r)+""+mathmlHilf(denominator(d),r)+"";} } } else { g=g+mathmlLeadCoef(string(leadcoef(a))); } setring(r); if (a==1 || a==-1) {g=g+"1";} g=g+mathmlMonomial(leadmonom(a)); a=a-lead(a); if (a!=0) { if (n==0) {if (poly(leadcoef(a))>0) {g=g+"+";}} else {if (SignumLeadcoef(numerator(leadcoef(a)))==0) {g=g+"+";}} g=g+mathmlPolyN(a,r); } return(g); } proc mathmlPoly(poly d) { string g=""; if (mathmlRingType()==0) {g=g+mathmlPolyR(d);} if (mathmlRingType()==2) {g=g+mathmlPolyGAL(d);} if (mathmlRingType()==1) {g=g+mathmlPolyN(d,basering);} g=g+""; return(g); } example { "EXAMPLE:"; echo=2; string h; ring r = (0,a),(x1,x2),dp; h=h+mathmlPoly(x1+a2)+"
"; h=h+mathmlPoly(x1*x2^3)+"
"; ring r=(0,a,b),(x(1..2)),lp; poly z=(3*a^2-12*b)*x(1)^2*x(2)+(3*a^3-1)*x(2)+x(1)^2-3/5*x(1)+1; h=h+mathmlPoly(z)+"
"; ring r=(0,a),(x,y),lp; poly z=(15*a^3+11*a-2)/(7*a^2+2)*x^2*y^3+13/7*x; h=h+mathmlPoly(z)+"
"; ring r=(13,s(1..2)),(y(1..2)),lp; poly z=(2*s(1)^4)*y(1)^3*y(2)-s(1)*s(2)*y(2)^4+(11*s(1)^3-2*s(2)^2+11*s(1)*s(2))*y(1)^2-s(2)*y(2)-3*y(2)+4; h=h+mathmlPoly(z)+"
"; ring r=(11,y(1..2)),(x(1..3)),lp; poly z=2*x(1)^3*x(3)-(2/(y(1)^2*y(2)^2+3))*x(2)^2; h=h+mathmlPoly(z)+"
"; ring r=integer,(v,x,y),lp; poly z=15*v^2*x^2*y^2-11*x*y; h=h+mathmlPoly(z)+"
"; ring r=(3^4,a),x,lp; poly z=a59*x2+a41; h=h+mathmlPoly(z)+"
"; ring r=real,(x,y),lp; poly z=3.334*x^2*y^3-4*y^2; h=h+mathmlPoly(z)+"
"; ring r=(real,15,a),(x(1..4)),lp; poly z=(5.2+11.11*a)*x(1)*x(4)^4-(2.333+1.344*a)*x(2); h=h+mathmlPoly(z)+"
"; ring r=complex,x,dp; poly z=(3.4-3.5*i)*x^5+(11.4*i)*x^4-3.552*x^3+1; h=h+mathmlPoly(z)+"
"; ring r=(complex,a),(ba,bb,bc),dp; poly z=(3.4-11*a)*ba^2*bb^2+4.555*ba^3; h=h+mathmlPoly(z); viewMathml(h); } /********************************************************* ** ** ** ring ** ** ** *********************************************************/ proc mathmlRing(def r) "USAGE: texRing(r); where r is a ring ASSUME: r is a ring RETURN: lateX-code for the ring r THEORY: KEYWORDS: ring, lateX, teX, tex, latex EXAMPLE: example texRing; shows an example " { setring r; list l,l1,l2; l = ringlist(r); l2 = ringlist(r)[2]; string c,p,P,V,C,S,h; int i,b; int bchar; list l3=l; l3[4]=ideal(0); def w=ring(l3); setring w; list l=fetch(r,l); setring r; //characteristic c = charstr(r); bchar = 0; if(find(c,",") != 0){ c = c[1,find(c,",")-1]; b = 0; if(typeof(l[1][2][1]) != "string" && b==0&& c =="integer"){bchar= 1;b = 1;} if(typeof(l[1][2][1]) != "string" && b==0){bchar = 2;b=1;} if(b==0){ if (mathmlRingType()==2) {bchar = 4;} else {bchar=3;} } } //minimalpolynom p = string(minpoly); //parameters if(bchar==3){ if(p != "0"){P=P+"[";} else{P=P+"(";} for(i=1;i(") {h=h[11..size(h)-10];} P=P + "]/{"+h+"}"; setring(r); } else{P=P+")";} } //variables V = V + "["; for(i = 1;i"+string(l[1][2][1])+""+string(l[1][2][2])+""; } else{C=C+""+""+string(l[1][2][1])+"";} b = 1; } else{C = C + "";b=1;} //Z } if(c == "0" && b == 0){C = C + "";b=1;} //Q if(c == "real" && b == 0){C = C + "";b=1;} //R if(c == "complex" && b == 0){ b=1; if(typeof(l[1][3]) != "none"){ if(l[1][3]!="i"){C = C + "("+l[1][3]+")";} //R(j) else{C = C + "";} } else{C = C + "F"+c+"";} //F/p else {C = C + ""+c+"";b=1;} //Z/p } //epic conclusion if(size(P)!=0){S = S + C+P+V;} else{S=S+C+V;} if (l[4]!=0) { setring w; S=S+"/("; for(int k=1;k<=size(l[4]);k++) { h=mathmlPoly(l[4][k]); h=h[48..size(h)-7]; S=S+h+","; } S=S[1,size(S)-10]+")"; } S = "" + S + ""; return(S); } example { "EXAMPLE:"; echo=2; string h; ring r = 32003,(x,y,z),dp; h=h+mathmlRing(r)+"
"; ring r = 32003,(x(1..10)),dp; h=h+mathmlRing(r)+"
"; ring r = 0,(a,b,c,d),lp; h=h+mathmlRing(r)+"
"; ring r =7,(x,y,z),ds; h=h+mathmlRing(r)+"
"; ring r = 7,(x(1..6)),(lp(3),dp); h=h+mathmlRing(r)+"
"; ring r =0,(x,y,z),(c,wp(2,1,3)); h=h+mathmlRing(r)+"
"; ring r =(7,a,b,c),(x,y,z),Dp; h=h+mathmlRing(r)+"
"; ring r =(7,a),(x,y,z),dp; minpoly = a^2+a+3; h=h+mathmlRing(r)+"
"; ring r =real,(x,y,z),dp; h=h+mathmlRing(r)+"
"; ring r =(real,50),(x,y,z),dp; h=h+mathmlRing(r)+"
"; ring r =(real,10,50),(x,y,z),dp; h=h+mathmlRing(r)+"
"; ring r = (complex,30,j),(x,y,z),dp; h=h+mathmlRing(r)+"
"; ring r =complex,(x,y,z),dp; h=h+mathmlRing(r)+"
"; ring r =7,(x,y,z), dp; h=h+mathmlRing(r)+"
"; ring r =integer,(x,y,z), dp; h=h+mathmlRing(r)+"
"; ring r =(integer, 6, 3),(x,y,z), dp; h=h+mathmlRing(r)+"
"; ring r =(integer, 100),(x,y,z), dp; h=h+mathmlRing(r)+"
"; ring r=(121,a),x,lp; h=h+mathmlRing(r); viewMathml(h); } /********************************************************* ** ** ** map ** ** ** *********************************************************/ proc mathmlMap(map f, list #)"USAGE: texMap(f, #); #[1] source XOR texMap(f),if source == target ASSUME: f is a map, #[1] ring or empty RETURN: lateX-code for the map f THEORY: KEYWORDS: map, lateX, teX, tex, latex EXAMPLE: example texMap; shows an example " { // f: s--->r (rings) || f:s1--->s2 (string) string s1,s2; string h1,h2; string T,S; //variables, output string s2 = mathmlRing(basering); s2 = s2[48,size(s2)-54]; int B = 0; int b = 0; if(nameof(basering)==nameof(preimage(f))){b=1;} def r = basering; if(size(#)!=0){ if(typeof(#[1]) != "ring"){return("ERROR - wrong input. check texMap documentation." );} def s; s = #[1]; setring s; s1 = mathmlRing(s); s1 = s1[48,size(s1)-54]; list l1; for(int k=1;k<=nvars(s);k++){ l1[k] = mathmlVariablesEncoding(string(var(k))); } setring r; for(int i = 1;i<=nvars(s);i++){ h1=mathmlPoly(f[i]); h2=h1[48,size(h1)-54]; T =T+ ""+l1[i]+""+""+""+h2+""; } } else{ if(b==1){ mathmlMap(f,basering); return(); } else{ s1 = "'"+nameof(preimage(f))+"'"; print("For detailed information we need the source"); B = 1; } } S = ""+s1+"" + "" +""+ s2+""+T; S = "" + S + ""; return(S); } example { "EXAMPLE:"; echo=2; string h; ring U = 0,(x,y,z),lp; ring M2 = real,(z(1..3)),lp; setring M2; map f = U,z(1)*z(2)^3,z(1),-z(2); h=h+mathmlMap(f)+"
"; //2nd h=h+mathmlMap(f,U)+"
"; //3rd ring R = real,(x,y,z),lp; map f2 = R,x2,y2,z-x; h=h+string(mathmlMap(f2)); viewMathml(h);} /********************************************************* ** ** ** IDEAL ** ** ** *********************************************************/ proc mathmlIdeal(ideal I)"USAGE: texIdeal(I), where I is an ideal ASSUME: I is an ideal RETURN: lateX-code for the ideal I THEORY: KEYWORDS: ideal, lateX, teX, tex, latex EXAMPLE: example texIdeal; shows an example "{ string S = ""; for(int i = 1;i⟩"; return(S); } example {"EXAMPLE: "; echo=2; ring r = 0,(x,y,z),dp; ideal I = x2,y3,z; viewMathml(mathmlIdeal(I)); } /********************************************************* ** ** ** LIST ** ** ** *********************************************************/ proc mathmlList(list l)"USAGE: texList(l), where l is a list ASSUME: l is a list RETURN: lateX-code for the list l THEORY: KEYWORDS: list, lateX, teX, tex, latex EXAMPLE: example texList; shows an example "{ string tmp; string S = ""; for(int i=1;i}";; return(S); } example {"EXAMPLE:"; echo=2; string h; //int ring r = integer,(x,y,z),dp; list l = 1,2,3; h=h+mathmlList(l)+"
"; //ring ring r2 = real,(a,b,c),dp; ring r3 = complex,(x(1..3)),dp; list l2 = r,r2,r3; h=h+mathmlList(l2)+"
"; //matrix setring r; matrix m1[2][2]=x2y,-xz3,0,17x3z2; matrix m2[2][2]=1,2,3,4; list l3 = m1,m2; h=h+mathmlList(l3); //poly poly p = x2-1; poly p2 = x3-y+z; poly p3 = y3-z7; list l4 = p,p2,p3; //mathmlList(l4); viewMathml(h); } /********************************************************* ** ** ** BIGINT,INT ** ** ** *********************************************************/ proc mathmlBigInt (bigint I){ return(""+string(I)+""); } proc mathmlInt(int I){ return(mathmlBigInt(I)); } /********************************************************* ** ** ** STRING ** ** ** *********************************************************/ proc mathmlString(string m){ return(m); } /********************************************************* ** ** ** NUMBER ** ** ** **requires: mathmlPoly - since there might be a parameter ** *********************************************************/ proc mathmlNumber(number n){ string S; S = mathmlPoly(n); return(S); } /********************************************************* ** ** ** VECTOR, INTVEC ** ** INTMAT, BIGINTMAT, MATRIX ** ** ** *********************************************************/ proc mathmlVector(vector v){ int L = size(v); matrix m[L][1] = v; string S = mathmlMatrix(m); return(S); } proc mathmlIntvec(intvec v){ matrix m = v; string S = mathmlMatrix(m); return(S); } proc mathmlIntmat(intmat M){ matrix m = M; string S = mathmlMatrix(m); return(S); } proc mathmlBigintmat(bigintmat M){ matrix m = M; string S = mathmlMatrix(m); return(S); } proc mathmlMatrix(matrix m){ string s,h; int i=1; int j=1; s=s+"("; while (i<=nrows(m)) { j=1; s=s+""; while (j<=ncols(m)) { h=mathmlPoly(m[i,j]); s=s+""+h[48,size(h)-54]+""; j=j+1; } s=s+""; i=i+1; } s=s+")"; return(s); } /********************************************************* ** ** ** View ** ** ** *********************************************************/ proc viewMathml(string s){ string p = "firefox"; string Sstart = "MathML Examples"; string Send = " "; string S = Sstart + s + Send; write(":w singular_mathml.html", S); system("sh",p+" singular_mathml.html"); }