Mathematica Miscellaneous Questions

  1. How do I create the union of two lists?
  2. How can I graph a set of points in the complex plane?
  3. Can MMA tell me the order of a complex pole?
  4. Can I do conformal mapping problems with MMA?
  5. Is there a way to factor polynomials with imaginary roots?
  6. How to get mathematica to write Exp[I x] in place of Cos[x] + I Sin[x].
  7. How can I use MMA to do Fourier Transforms?
  8. How can I use MMA to do Laplace Transforms?
  9. How can I find a list of all the escape key sequences (like for the fraction bar)?
  10. Can I define piecewise functions with MMA?
  11. How do I simplify something like Cos[m PI] where m is an integer?
  12. How can I pass variables to MMA from a C program?
  13. Is it possible to give MMA command line instructions?
  14. How do I tell MMA to leave the text formatting alone inside my (* comments *)?
  1. Suppose I have a couple of definitions in a file:

    a={ {1,2}, {3,4}, {5,6} }     b={ {7,8}, {9,A}, {B,C} }

    How can I create an object c that is the union of these two?

  2. Union[ a, b ]
    In[1]:= a={ {1,2}, {3,4}, {5,6} }; b={ {7,8}, {9,A}, {B,C} };
    In[2]:= c = Join[a, b]
  3. I have a complex valued funtion with a singularity at x=1:

    f[z_] := NIntegrate[ (x^2 -1}^(-3/4), {x, 0, 1, z} ]

    I have been creating a table of values using the Table command:

    m = Table[f[i], {i,0,10, .1}]

    which gives m a value that looks something like:

    m = { {1+2*I}, {10+3*I}, {20-5*I}, ...}

    How can I *graph* these points? I would like to graph the real part on the x axis and the imaginary part on the y axis. So in the example above, I would like to change the definition of m to be: m = { {1, 2}, {10, 3}, {20, -5}, ...}

  4. You could use:

       	 m = Map[ {Re[ #[[1]] ],Im[ #[[1]] ]} &, m ]


    In[8]:= m = { {1+2*I}, {10+3*I}, {20-5*I}};
    In[9]:= Flatten[ m /. Complex[a_, b_] :> {a,b}, 1]
    Out[9]= {{1, 2}, {10, 3}, {20, -5}}
  5. Can I use MMA to calculate the order of a complex pole? Can I get MMA to tell me if a singularity is essential, removable, or a pole?
  6. MMA supports Laurent series, so you can use Series to expand your function, say:

       In[1]:= (Exp[z^2] - Exp[z])/z^3 + O[z]^4;

    then to determine the order of the pole, turn the Laurent series into an ordin- ary expression and "invert" (ie take the reciprocal of) the expansion variable

       In[2]:= Normal[%] /. z -> 1/z
       Out[2]= -(1/6) + 119/(720*z^3) - 1/(120*z^2) + 11/(24*z) + z/2 - z^2

    and use Exponent to determine the "leading" term:

       In[3]:= Exponent[%, z]
       Out[4]= 2

    This is easily turned into a module:

       In[5]:= PoleOrder[f_, {z_, z0_, n_}] := 
       Module[{s = f + O[z, z0]^n, p}, 
       If[(p = Exponent[Normal[s] /. z -> z + z0 /. z -> 1/z, z]) > 0, p, 0]]

    PoleOrder handles arbitrary expansion points. Note that the input syntax of PoleOrder is analogous to Series. Here are some examples:

       In[6]:= PoleOrder[(Exp[z^2] - Exp[z])/z^2, {z, 0, 4}]
       Out[6]= 1
       In[7]:= PoleOrder[Exp[z^2] - Exp[z], {z, 0, 4}]
       Out[7]= 0
       (* Removable singularity: *)
    	In[8]:= PoleOrder[(z^2 - 1)/(z^3 - 1), {z, 1, 3}]
    	Out[8]= 0
       (*Pole: *)
    	In[9]:= PoleOrder[1/(z^3 - 1), {z, 1, 3}]
    	Out[9]= 1

    MMA automatically recognizes essential singularities; try entering Exp[1/z] + O[z] and see what happens (interestingly, in 3.0, apart from an essential singularity warning message, I get a messy InterpretationBox object; I think this is a bug/error).

    The full information of a SeriesData object may be used for this purpose.

       In[23]:= ??SeriesData
          SeriesData[x, x0, {a0, a1, ... }, nmin, nmax, den] represents a power 
          series in the variable x about the point x0. The ai are the coefficients 
          in the power series. The powers of (x-x0) that appear are nmin/den, 
          (nmin+1)/den, ... , nmax/den.  Attributes[SeriesData] = {Protected, 

    For example, nmin for 1/x^2, expanded at the origin, is -2.

    In[24]:= InputForm[ Series[1/x^2, {x,0,4}] ]
    Out[24]//InputForm= SeriesData[x, 0, {1}, -2, 5, 1]
  7. Can I do conformal mapping problems with MMA?
  8. A conformal map plotting package can be found at

  9. Is there a way to factor polynomials with imaginary roots?
  10. You can factor polynomials with imaginary roots with the extension option:

    	In[10]:= Factor[x^2 + 2*x + 10, Extension -> {I}]
    	Out[10]= (1 - 3*I + x)*(1 + 3*I + x)

    You can also use the GaussianIntegers option:

       F[x^2 + 2x + 10,GaussianIntegers->True]
  11. How to get mathematica to write Exp[I x] in place of Cos[x] + I Sin[x]. Suppose I have the expression:

         In[1]: f[x_]= Cos[x] + I Sin[x] - Cos[2 X] + I Sin[2 X]

    I would like mathematica to simplify this into

          f[x_]= Exp[I X] - Exp[-2 I X]

    I tried the substitution command:

           In[2]: % ./ Cos[X]+I Sin[X]-> Exp[I X]

    and it returned

           Out[2]: E^{I X}  - Cos[2 X] + I Sin[2 X]
  12. One needs two replacement rules:

    Cos[t_]+I Sin[t_]-> Exp[I t] 
    Cos[t_]-I Sin[t_]-> Exp[-I t].

    Using Pattern[t, Blank[]] (the t_) will take care of the different variable names x and 2 X, but there are still two substitutions required. One needs to use ReplaceRepeated[] (//.).

    Cos[x] + I Sin[x] + Cos[2 X] + I Sin[2 X]//.Cos[t_]+I Sin[t_]-> Exp[ I t]

    will work and so will:

    Cos[x] + I Sin[x] - Cos[2 X] + I Sin[2 X]//.{Cos[t__]+I Sin[t__]-> 
    Exp[I t], -(Cos[t_]-I Sin[t_])-> -Exp[- I t]}

    This still seems clumsy and I hope someone will suggest a more efficient way of writing this.

  13. Can MMA do Fourier Transforms?
  14. With Mma 3.0, press F1 (help key) and check the add-ons: With Mma 2.x, check The Book:

    In[1]:= <<Calculus`FourierTransform`
    In[2]:= FourierTransform[Cos[2x], x, t]
    Out[2]= Pi (DiracDelta[-2+t]+DiracDelta[2+t])
  15. Can MMA do Laplace Transforms?
  16. Sure, for example:

         LaplaceTransform[Sin[t], t, s]
         LaplaceTransform[f'[t] + -t^2 g[t], t, s]
  17. How can I find a list of all the escape key sequences (like for the fraction bar and exponentiate palette key?
  18. For the complete list of short-cuts, have a look at Among the more interesting examples you'll see things like \[KlingonA].

  19. How do I define a piecewise function?;
  20. f[r_,theta_] := {r^2,1/r} /; r < 10
    f[r_,theta_] := {Cos[r],theta*r} /; r > 10 
    f[r_,theta_] := 0 /; r == a
    Simplify[{Cos[m Pi], Sin[m Pi] }, {m \[Element] Integers}] // InputForm
    {(-1)^m, 0}
  21. From within a C program, I would like to run mathematica and pass an array to it. I know that system(math); will run MMA, but how can I pass variables to a process run by system?
  22. You can do this via MathLink. ML is a communication protocol developed by WRI. If you've used MMA's X Front End on your system (or a notebook front end on any other platform), you've seen probably the largest ML application out there. The notebook front end communications with the MMA kernel using MathLink. You may want to look at href=" for more info.

    Also, "A MathLink Tutorial" by Todd Gayley is an excellent document describing ML. You can find this as a PS file and as a notebook on MathSource - MathSource is WRI's electronic library of MMA related docs and is available here. Just do a search for the key word 'mathlink' and it'll return a list of links pointing to stuff related to MathLink. One of them will point to the tutorial. You'll also see some example ML code on there. MathSource docs are free! ML ships with all professional versions of MMA. If you can't find it on your system, the first link above presents a page with a link pointing to the developers kits that you can down load. There are sample ML projects on the developer's kits page.

    You may also check with WRI's support ( or if your insti- tution has a central distribution, you may want to check with them.

    I think you're on the right track with the system command. If the command you would type normally on the command line is math 2 + 2, then in C, the system command would look like: system("math 2 + 2"); If you want to use variables to construct the command, the you can build the string using strcpy and str cat, eg,

    char	cmd[80],line[64];
    strcpy(cmd,"math ")
    printf("Enter math argument(s): ");

    Sure it is possible. And you can even process entire notebooks as documented in the manual. One trick is to use the option math -batchinput to process the entire notebook. The other is to begin with the command PrependTo[$Echo, "stdout"] to see the input grouped with the output in the resulting file.

  23. I am on a sun machine running sunos 4.*. Is it possible to give MMA command line instructions? something like:

    math << 'Plot[ Sin[x], {x,0,2*Pi} ]'

    if so, can i put it in the background and save the output to a file?

    nohup math << 'Plot[ Sin[x], {x,0,2*Pi} ]' > save_file &
  24. To run MMA on the background I use the following procedure. The aim is to start MMA at a specific time, run MMA commands which are stored in a file, store the results in another file, then exit MMA.

    The procedure has nothing to do with Mathematica itself, but with the Unix OS. In other words, the same can be done to any application. The Unix 'at hhmm' command starts off a process at military time hhmm. The construct ends with ^D (Control key + D). The construct '<< eof ... eof' tells the shell that the commands are input for Mathematica rather than for the shell itself.

    A working code is as follows. Suppose that at 4.15 pm we want MMA do run the code in file 'MathRemoteCode.txt', store the results of the computations (a list) in file 'computedList1645010297' and finally exit.

    'at' command:           at 1645
                            math << eof
                            Exit[ ]
    contents of 'MathRemoteCode.txt'
    Table[ Random[ ], {10} ] >> computedList1645010297
    contents of 'computedList1645010297'
    {0.2165258208963744, 0.4787984041170798, 0.6907162037992087, 0.99395320442717, 
    0.0887044807992424, 0.5125555633332101, 0.97840724468237, 0.4060014270165736, 
    0.481372399489035, 0.2925335958104831}   

    The front end has built in styles. You can use Section, Subsection etc.forsectioning. As well you may use Text style for your comments. All these cells are not evaluated and can be formatted very nicely. Use Input (usually the default style) for your code.