(*-------------------------code.m------------------------------*) (* code.m contains: rules for complex algebra manipulation *) (* a mathematica function "code" that *) (* allows the user to input a function of *) (* complex variables and outputs function *) (* first and second order derivatives *) (* in Fortran code to the files .gk , .ak *) (* and .hk respectively *) (*-------------------------------------------------------------*) (*------------------------------------------------------*) (* c[] is used to denote the conjugate of the values *) (* inside the brackets. *) (*------------------------------------------------------*) c::usage = "c[z] represents the complex conjugate of z" c/: c[(x_) + (y_)] := c[x] + c[y] c/: c[(x_)/(y_)] := c[x]/c[y] c/: c[-(x_)] := -c[x] c/: c[(x_)*(y_)] := c[x]*c[y] c/: c[ x_ ^ n_Integer ] := c[x] ^ n c/: c[c[x_]] := x c/: c[x_Integer] := x c/: c[x_Real] := x c/: c[x_Rational] := x c/: c[z_Complex] := Conjugate[z] c/: c[Exp[z_]] := Exp[c[z]] (*-----------------------------------------------------------*) (* re[z] returns the real part of a complex argument z *) (*-----------------------------------------------------------*) re::usage = "re[z] returns an expression for the real part of z" re/: re[z_] := (z + c[z])/2 (*-----------------------------------------------------------------*) (* im[z] returns the imaginary part of a complex argument z *) (*-----------------------------------------------------------------*) im::usage = "im[z] returns an expression for the imaginary part of z" im/: im[z_] := (z - c[z])/(2*I) (*---------------------------------------------------------*) (* c'[z_] := 0 so that all conjugate values are treated as *) (* constants in differentiation *) (*---------------------------------------------------------*) c'[z_] := 0 (*--------------------------------------------------------*) (* cabs[z] returns Abs[z] if z is real valued otherwise *) (* cabs[z] returns z c[z]^(1/2) *) (*--------------------------------------------------------*) cabs::usage ="cabs[z] returns (z c[z])^(1/2)" cabs/: cabs[z_Real] := Abs[z] cabs/: cabs[z_] := (z*c[z])^(1/2) cabs2/: cabs2[z_Real] := Abs[z] cabs2/: cabs2[z_]^(n_) := ( z * c[z] )^(n/2) /; EvenQ[n] cabs2/: cabs2[z_]^(n_) := cabs2[z] ( z * c[z] )^((n-1)/2) /; OddQ[n] cabs2/: cabs2[z_] := Sqrt[ re[z]^2 + im[z]^2 ] (*-------------------------------help--------------------------------------*) help/: help := Run[ helpme] (*---------------------------------------------------------------------------*) (*-------------------------------example1------------------------------------*) example1/: example1 := ( Print["**************************************************************"]; Print["* To continue: hit [return]"]; Print["*"]; InputString["* In[1] = g = cabs[ 0.5 - (z[1] + 1/e)^2 ]^2"]; Print["*"]; Print["* 1 2 1 2"]; Print["* Out[1] = (0.5 - (---- + c[z[1]]) ) (0.5 - (- + z[1]) )"]; Print["* c[e] e"]; Print["*"]; InputString["* In[2]= code"]; InputString["* Number of Dimensions? 1"]; Print["*"]; Print["* START"]; Print["* END"]; Print["*"]; InputString["* Do you wish to quit controller? (y=yes,n=no): n"]; Print["*"]; InputString["* Do you wish to edit the resulting functions? (y=yes,n=no): n"]; Print["*"]; Print["* This will take a few seconds to compile and link the program."]; Print["* ... ... ... ..."]; Print["* [Here, compiling messages appear on the screen.]"]; Print["* ... ... ... ..."]; Print["*"]; Print["* Code passed the check!"]; Print["*"]; Print["* dsaprx is ready for execution."]; Print["*"]; Print["* %"]; Print["*..................Example running dsaprx......................."]; Print["*"]; InputString["* % dsaprx ---------------------------------------------"]; Print["* 8"]; Print["*"]; InputString["* In[2]= code"]; InputString["* Number of Dimensions? 1"]; Print["*"]; Print["* START"]; Print["* END"]; Print["*"]; InputString["* Do you wish to quit controller? (y=yes,n=no): n"]; Print["*"]; InputString["* Do you wish to edit the resulting functions? (y=yes,n=no): n"]; Print["*"]; Print["* This will take a few seconds to compile and link the program."]; Print["* ... ... ... ..."]; Print["* [Here, compiling messages appear on the screen.]"]; Print["* ... ... ... ..."]; Print["*"]; Print["* Code passed the check!"]; Print["*"]; Print["* dsaprx is ready for execution."]; Print["*"]; Print["* %"]; Print["*..................Example running dsaprx......................."]; Print["*"]; (* InputString["* % dsaprx c[e] /. z[i_] -> c[z[i]]; Do[ ak[j] = D[g , z[j] ]; hk[j] = D[g , z[j] , c[z[j]] ] , {j , n}]; OpenRead["emath.m"]; OpenRead["zmath.m"]; points = Read["emath.m"]; OpenWrite["ak.m", FormatType -> FortranForm]; OpenWrite["g.m", FormatType -> FortranForm]; OpenWrite["hk.m", FormatType -> FortranForm]; OpenWrite["param.m", FormatType -> FortranForm]; Write["ak.m", n]; Do[ a[j] = Random[] + I Random[]; Write["param.m", N[a[j], 16] ] , {j , 10}]; Do[ e = Read["emath.m"]; Do[ z[j] = Read["zmath.m"] ,{j , n}]; If[ g == gp , , test = True]; Write["g.m", N[Re[g], 16] ]; Do[ Write["ak.m" , N[ak[j], 16] ]; Write["hk.m" , N[Re[hk[j]], 16] ]; , {j , n}]; ,{i , points}]; e =.; Do[ z[j] =. , {j ,1,n}]; Do[ a[i] =. , {i,1,10}]; ch = {z[k_Real] -> F[i,Round[k]], e -> E[i], c -> DConjg, a[k_Real] -> a[Round[k]]}; swap = E^z_ -> CDEXP[z]; Print[""]; Print[START]; OpenWrite["../.gk"]; WriteString["../.gk", " Gk(i) = \n"]; WriteString["../.gk"," -"]; Close["../.gk"]; OpenAppend["../.gk" , FormatType -> FortranForm, PageWidth -> 74]; Write["../.gk" , N[DREAL[ g /. swap]]/. ch]; Close["../.gk"]; Do[ jj = ToString[j]; file = StringJoin["../.ak" , jj]; OpenWrite[file]; string = StringForm[" Ak(i,``) = ", j]; WriteString[file , string]; WriteString[file ,"\n -"]; Close[file]; OpenAppend[file , FormatType -> FortranForm, PageWidth -> 74]; Write[file , N[D[g , z[j] ] /. swap]/. ch]; Close[file] ,{j , n} ]; Do[ jj = ToString[j]; file = StringJoin["../.hk" , jj]; OpenWrite[file]; string = StringForm[" Hk(i,``) = ", j]; WriteString[file , string]; WriteString[file ,"\n -"]; Close[file]; OpenAppend[file , FormatType -> FortranForm, PageWidth -> 74]; Write[file, N[ DREAL[ D[ g , c[z[j]] , z[j] ] /. swap ]]/. ch]; Close[file] ,{j , n} ]; Print[END]; If[ test, Print[""]; Print["Warning: Function not Real Symmetric"]]; Exit[] ) (*---------------------------------------------------------------------*)