3D plots with sagetex and pgfplots

ThreeDgraphingIn previous posts, here, here, and here, we've seen how Sage's ability to calculate points for functions makes it a viable engine (and convenient alternative due to Sagemath Cloud) to generate Tikz/pgfplots pictures, rather than gnuplot. Actually, Sage and sagetex are an improvement over gnuplot. Don't believe me? Take a look at this post here at TexStackExchange and you might get the impression that for complicated graphs you're "better off" with Asymptote/PSTricks because pgfplots doesn't have any "numerical integration schemes". Now look at the post which it is said to be a duplicate of and you'll see more discussion of why pgfplots is not as effective as PSTricks. In both questions the users specifically asked a question about how they should plot something in Tikz/Pgfplots and in the only case where an answer was accepted the user settled for a PSTricks solution. In that second post a comment to the original questions says, "If you want to use TikZ then, you could use gnuplot. It doesn't support integration natively, but you can find examples easily googling them.".  If you've ever seen this post you'll get more details on things that PSTricks can do that Tikz struggles at. In the comments to that question, Alain Mathes (the designer of the Altermundus packages) writes, "The limits actually of TikZ is pgfmath. Calculus with Tex are not efficient and often very slow. We can do a lot of things with TeX but we can't compare calculus and programming with postscript and the same thing with TeX. Now perhaps lua and luatex can change something. With lua and some modules we can do I think everything but I don't how if the compilation is not too slow ?".

It should now be clear that there are weaknesses with the math skills of Tikz and gnuplot; the only issue seems to be overhead (Sage is big, Lua is small...perhaps a stripped down Sage version can be made?).

But go back to that first post I mentioned. I submitted the only answer that answered the question using pgfplots by relying on Sage and the sagetex package. Sagemath Cloud gets them to work with LaTeX.  So it seems clear (gnuplot's limitations of integration aren't a problem with Sage) that Sage should, at the very least, be an option in Tikz/Pgfplots. See, for example, the gallery of pgfplots examples and check out how gnuplot is called:



\addplot3[contour gnuplot] {x*y};



Why not an option for contour Sage and have Sage do the work? That is:

\addplot3[contour sage] {x*y};

That built in support is needed because the work-arounds like I showed are just a little too difficult--the PSTricks answer was not only accepted by the user (who wanted a pgfplots solution) but it got more upvotes than the only solution that answered the question. What's up with that?!?!?

I've added another "proof of concept" example using sagetex, sage and pgfplots to the Plotting with Sagetex page. The example, shown above, illustrates how 3D graphs can be created. The extra complication with 3D graphs is you need to indicate the number of rows of data; but Sage can take care of that as well. Change step from 0.20 to 0.25 and the mesh will adjust. You can find the code here that you can use as a template. If and when these package developers start using Sage (with/without gnuplot) as the computational engine then plotting in LaTeX will take a big step forward.

SageTex: Combinatorics

CombProb2I've added another problem to the Sagetex: Combinatorics/Probability page. For this problem the user chooses a word (in capital letters), such as CALCULUS shown above, and the problem asks how many different arrangements are there using all the letters. The solution will create a table showing the distribution of the letters and print out the answer both as a formula and as a number.

Complications arise from trying to get the the answer in both forms: if you try to typeset the answer outside of sagesilent then you don't have knowledge of which letters were repeated how many times. This needs to be done inside sagesilent and is calculated here (formatting is not accurate).

numdenom = 1
answer = r"\frac{%d!}{"%(letters)
for i in range(0,26):
if repetitions[i] > 0:
answer += r"%s!"%(repetitions[i])
numdenom *= factorial(repetitions[i])
answer += r"}"
numanswer = factorial(letters)/numdenom

Note that the numerical value of the denominator (numdenom) is calculated at the same time.

This is a common theme I've found with sagetex: to get the most out of the package you need to be able to create LaTeX code inside a sagesilent block.

Odds and Ends: Feb 11,2014

TriAreaAn assortment of topics today.

1. I've taken the Sage interact manipulative that involved using the determinant to find the area of a triangle defined by 3 points and made it into a Sagetex problem. The output is above and it's been added to the Sagetex Matrices page. Yet another problem for what will become a database of random problems with solutions that can be put into your test, especially if you're using the sagetex test template I've created.

2. What's the sum $latex \sum_{i=1}^{\infty}i=1+2+3+4+5+6+\cdots$

Infinity, you say (or maybe "that's a divergent series")? Actually, it's $latex -\frac{1}{12}$, and the Numberphile website has the math to prove it. They've been mentioned in an article by the NY Times and the video I'm talking about is embedded there. The answer of $latex -\frac{1}{12}$, "as absurd as it sounds, has been verified to many decimal places in lab experiments."...."But there is broad agreement that a more rigorous approach to the problem gives the same result, as shown by a formula in Joseph Polchinski’s two-volume textbook “String Theory.”"

Continuing from the NY Times article, "In modern terms, Dr. Frenkel explained, the gist of the calculations can be interpreted as saying that the infinite sum has three separate parts: one of which blows up when you go to infinity, one of which goes to zero, and minus 1/12. The infinite term, he said, just gets thrown away.

And it works. A hundred years later, Riemann used a more advanced and rigorous method, involving imaginary as well as real numbers, to calculate the zeta function and got the same answer: minus 1/12.".

That's all over my head, but throwing away "the infinite term" doesn't sound quite correct mathematically, even if it makes the physics work.

3. If you are following the world of Common Core, the opposition keeps growing. It's been a mess from my first row seat, too, and the criticism is coming from all sides:

Some brutally honest comments by [Secretary of Education Arne] "Duncan said he found it "fascinating" that opposition has come from "white suburban moms who -- all of a sudden -- [learned] their child isn't as brilliant as they thought they were, and their school isn't quite as good as they thought they were."" haven't helped.

But ultimately, it's not that surprising. Education movements come and go, each one promising to make things better before it is ultimately abandoned for something else. This is the year "... all U.S. public schools were going to reach 100 percent student proficiency, thanks to No Child Left Behind (NCLB).".  I liked the opinion voiced here, "When students complete eight years of learning (elementary and middle school) and are unable to achieve the percentile of literacy that is required, they should then be permitted to attend a tech school where the emphasis would be on learning a job skill that would help them become employable when they graduate."

4. Nevada has addressed the issue of the inability of students to meet proficiency exams head on (yes, that's sarcasm) by making them easier to pass.

"Board member Dave Cook of Carson City said research has shown no evidence that students who pass the exams are more successful in college or career, so “why prolong them?”

“This is a totally subjective process forced upon students when it is of no benefit,” he said.". Which makes you wonder why they forced students to go through the "totally subjective process" to begin with. Sounded like a good idea at the time, I'll bet.

"The board will likely decide on what the new passing score will be Feb. 26, when a test vendor presents the estimated minimum score needed to maintain a 54 percent pass rate among sophomores. Students first take the test their sophomore year and can retake the test throughout high school until they pass.

This year, 56 percent of sophomores scored at least 252 on their first, so the new minimum will likely be around there, state officials said.". So set the standard low enough that it can be met now, whether or not (it's not) that level is an appropriate standard. Sigh.

pgfplots with sagetex

SageTikZIn earlier posts I used the sagetex package to force sage do the calculations for the Altermundus packages (rather than gnuplot). With Sagemath Cloud putting Sage at your fingertips you don't need to go through the hassle of gnuplot.

Although the Altermundus packages let you insert the output string between \begin{tizpicture}...\end{tikzpicture}, pgfplots isn't so obliging. I've managed to get that working as well but it requires making the output string contain everything from \begin{tizpicture} to \end{tikzpicture}. This use of sagetex creates the data on the fly, so there's no need for an external data file. You can see the code running above; the Plotting with Sagetex page has more details and the code to download. Just change the function and plotting parameters to what you want and you'll have Sage crunching the math. The Plotting with Sagetex page also includes Dr. William Stein's instructions on how to increase the buffer for Sagemath Cloud.


Plotting a function and its derivative with tkz-fct


In trying to adapt the sagetex package to the beautiful packages of Altermundus there are some rough patches: so many macros rely on (at some level) the Gnuplot program. I added an example to the Plotting with Sagetex page to address some issues that come up. The biggest problem is that if a function has a large derivative then there is an issue that the graph (red curve above) doesn't go to the edge of the screen because the step size is too small--but increasing the step size results in too many points and it can't be graphed. I can fix this on my local tex installation of by changing the buffer size to a bigger number (from 200000 to 1000000). It takes a longer time for sagetex to process but eventually you can get the desired output:

Alt2I don't know how to fix that on Sagemath Cloud, though. You can read more of the details by clicking here.