I am using Mathematica to produce code that can be pasted into Meep, an EM field simulator. I have a list of random numbers and a list of radii (all identical), and I need a very specific output. For example say the list of random reals is {0.1, -0.2, 0.4} and my radii are {0.8,0.8,0.8}. I need to output {0.1+0.8, -0.2+0.8, 0.4+0.8} and then put each element into a column of my table. Preferably the addition shouldn't be evaluated to {0.9,0.6,1.2}, although that's not essential.

Here's the code I'm using to generate my table:

R = ConstantArray[0.8, 3]
tolerance = RandomReal[{-0.006, 0.006}, 91]  
{Print["(set! geometry-lattice (make lattice (size ", sx , " ", sy , 
" no-size)))"];, 
table1 = Table[Print["(make cylinder (radius " , R, " ) "];, 3]}

The output I get is

(set! geometry-lattice (make lattice (size 1.842 1.59522 no-size)))

(make cylinder (radius {0.8,0.8,0.8} ) 

(make cylinder (radius {0.8,0.8,0.8} ) 

(make cylinder (radius {0.8,0.8,0.8} ) 

But ideally I need it to output

(set! geometry-lattice (make lattice (size 1.842 1.59522 no-size)))

(make cylinder (radius 0.1+0.8) 

(make cylinder (radius -0.2+0.8) 

(make cylinder (radius 0.4+0.8)

That would be in a scenario where the first element of the list 'tolerance' is 0.1, the second element of the list is -0.2 and the third is 0.4.

I've tried several approaches and got to a point where I could live with an output where e.g. the 0.1+0.8 is evaluated to 0.9, which is why I thought I would turn my single radius into a list with each element being that radius, add it to the tolerance outside of the table using just

R + tolerance  

But then I can't find a way to put each successive element into successive rows of the table.

Apologies for a long and convoluted question, and thanks for any suggestions!

  • 1
    $\begingroup$ Look up TableForm: for instance TableForm[R + tolerance] $\endgroup$ – bill s Jan 9 '18 at 23:33


table1 = Table[Print["(make cylinder (radius " , (R + tolerance)[[i]], " ) "];, {i, 3}]}


table1 = Table[Print["(make cylinder (radius " , R[[i]] + tolerance[[i]], " ) "];, {i, 3}]}

There's no point in assigning the result of Table[..] to table1. The value is just {Null, Null, Null}, which doesn't seem useful.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.