2
$\begingroup$

to bootstrap my experience with Mathematica I'm trying to use it to print a table of values where the columns are different trigonometric functions and the rows contain different the values of said trig functions at some fixed angles that are between $0$ and $\frac{\pi}{2}$ .

After studying the basics I came up with this lines of code

vec = Prepend[Table[Pi/i, {i, Reverse[Range[2, 11]]}], 0];
res = Map[#, vec] & /@ {Sin, Cos, Tan, Csc, Sec, Cot};
TextGrid [res, Frame -> All]

and the output is

$\begin{array}{ccccccccccc} 0 & \sin \left(\frac{\pi }{11}\right) & \frac{1}{4} \left(\sqrt{5}-1\right) & \sin \left(\frac{\pi }{9}\right) & \sin \left(\frac{\pi }{8}\right) & \sin \left(\frac{\pi }{7}\right) & \frac{1}{2} & \sqrt{\frac{5}{8}-\frac{\sqrt{5}}{8}} & \frac{1}{\sqrt{2}} & \frac{\sqrt{3}}{2} & 1 \\ 1 & \cos \left(\frac{\pi }{11}\right) & \sqrt{\frac{\sqrt{5}}{8}+\frac{5}{8}} & \cos \left(\frac{\pi }{9}\right) & \cos \left(\frac{\pi }{8}\right) & \cos \left(\frac{\pi }{7}\right) & \frac{\sqrt{3}}{2} & \frac{1}{4} \left(\sqrt{5}+1\right) & \frac{1}{\sqrt{2}} & \frac{1}{2} & 0 \\ 0 & \tan \left(\frac{\pi }{11}\right) & \sqrt{1-\frac{2}{\sqrt{5}}} & \tan \left(\frac{\pi }{9}\right) & \tan \left(\frac{\pi }{8}\right) & \tan \left(\frac{\pi }{7}\right) & \frac{1}{\sqrt{3}} & \sqrt{5-2 \sqrt{5}} & 1 & \sqrt{3} & \text{ComplexInfinity} \\ \text{ComplexInfinity} & \csc \left(\frac{\pi }{11}\right) & \sqrt{5}+1 & \csc \left(\frac{\pi }{9}\right) & \csc \left(\frac{\pi }{8}\right) & \csc \left(\frac{\pi }{7}\right) & 2 & \frac{1}{\sqrt{\frac{5}{8}-\frac{\sqrt{5}}{8}}} & \sqrt{2} & \frac{2}{\sqrt{3}} & 1 \\ 1 & \sec \left(\frac{\pi }{11}\right) & \frac{1}{\sqrt{\frac{\sqrt{5}}{8}+\frac{5}{8}}} & \sec \left(\frac{\pi }{9}\right) & \sec \left(\frac{\pi }{8}\right) & \sec \left(\frac{\pi }{7}\right) & \frac{2}{\sqrt{3}} & \sqrt{5}-1 & \sqrt{2} & 2 & \text{ComplexInfinity} \\ \text{ComplexInfinity} & \cot \left(\frac{\pi }{11}\right) & \sqrt{2 \sqrt{5}+5} & \cot \left(\frac{\pi }{9}\right) & \cot \left(\frac{\pi }{8}\right) & \cot \left(\frac{\pi }{7}\right) & \sqrt{3} & \sqrt{1+\frac{2}{\sqrt{5}}} & 1 & \frac{1}{\sqrt{3}} & 0 \\ \end{array}$

the problems with this output are :

  • some cells are in what it looks like a rational / expected form and others have a "weird" unevaluated form like $sin(\frac{\pi}{11})$ ; what is the problem with the latter ?
  • I haven't found a way to print legends for the rows and the columns ( angle values and function names )

Thanks

$\endgroup$
4
  • $\begingroup$ Not sure what you mean by "print legends" would it suffice to simply prepend your column / row values to res? Also that unevaluated form is simply the exact form for the expression. If it knows the exact ratio or whatever it'll return that, but otherwise Mathematica has a principle of remaining as exact and symbolic as possible. $\endgroup$
    – b3m2a1
    Commented Mar 16, 2017 at 18:19
  • $\begingroup$ @MB1965 basically any solution that could help me recognize rows and columns without making a mess with too much intricacies in the code . I would like to keep the computation separate from the the graphical layout . Regarding the 2nd part : can I force an approximation for the entire table ? $\endgroup$
    – user47392
    Commented Mar 16, 2017 at 18:23
  • $\begingroup$ Second part, use N. That's its purpose. First part, isn't bad either. We'll do a bit of prepending. I'll knock you up a quick solution and explanation. $\endgroup$
    – b3m2a1
    Commented Mar 16, 2017 at 18:25
  • $\begingroup$ @MB1965 thank you $\endgroup$
    – user47392
    Commented Mar 16, 2017 at 18:28

1 Answer 1

4
$\begingroup$

So we'll just prepend your labels or whatever onto your grid. Note that you can change the dividers for Grid based constructs too, if you want a different appearance.

Here are my changes:

  • Use Through to build your res in the transposed orientation
  • Use N on that to get the numerical approximations you wanted
  • Use Prepend to stick on the function labels, transpose the grid, then prepend on the number labels.

Looks like this in the end:

vec = Prepend[Table[Pi/i, {i, Reverse[Range[2, 11]]}], 0];
ops = {Sin, Cos, Tan, Csc, Sec, Cot};
res = Through@*ops /@ vec // N;
grid = Prepend[Transpose@Prepend[res, ops], Prepend[vec, Null]];
NumberForm[TextGrid[grid, Frame -> All], 3]

fig grid

A few things to note:

I use @* (Composition) to make Through apply after the functions and NumberForm to make sure the numerical approximations only display up to 3 digits.

$\endgroup$
5
  • $\begingroup$ @MB1965 I get the same result for res using Through[ops[#]] & /@ vec // N. I am not sure I understand the construct that you use for res. Can you enlighten me as to the differences, pros, cons? Thank you. $\endgroup$ Commented Mar 17, 2017 at 22:18
  • $\begingroup$ @JackLaVigne do you mean the Composition (@*)? What you did is another way to do it, but & is a very low precedence operator and so can often grab much more than what you intended. It just allows me to not have to use a pure function and is often clearer, in my mind. $\endgroup$
    – b3m2a1
    Commented Mar 17, 2017 at 22:21
  • $\begingroup$ @MB1965 Yes, the Composition. I see that it produces the desired result but can't quite figure out what is going on. Maybe you can spell it out for me? $\endgroup$ Commented Mar 17, 2017 at 22:23
  • $\begingroup$ @JackLaVigne I'd be happy to. f@*g in FullForm is Composition[f,g] and Composition[f,g][h] is f[g[h]]. Contrast that to f@g where we would get f[g][h]. It allows f to apply after g has applied instead of applying to g before hand. There is a corresponding head, RightComposition /* that is similar, but acts in reverse. Check out the docs for a more thorough treatment. $\endgroup$
    – b3m2a1
    Commented Mar 17, 2017 at 22:26
  • $\begingroup$ @MB1965 Got it. Map[Composition[Through, {Sin, Cos, Tan, Csc, Sec, Cot}], {x, y, z}] produces {{Sin[x], Cos[x], Tan[x], Csc[x], Sec[x], Cot[x]}, {Sin[y], Cos[y], Tan[y], Csc[y], Sec[y], Cot[y]}, {Sin[z], Cos[z], Tan[z], Csc[z], Sec[z], Cot[z]}}. Thank you for the help. $\endgroup$ Commented Mar 17, 2017 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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