Plotting a function with a lot of local minima

Stefan Steinerberger defines “an amusing sequence of functions” in [1] by

f_n(x) = \sum_{k=1}^n \frac{|\sin( k\pi x )|}{k}

Here’s a plot of f30(x):

As you can see, fn(x) has a lot of local minima, and the number of local minima increases rapidly with n.

Here’s a naive attempt to produce the plot above using Python.

    from numpy import sin, pi, linspace
    import matplotlib.pyplot as plt

    def f(x, n):
        return sum( abs(sin(k*pi*x))/k  for k in range(1, n+1) )

    x = linspace(0, 1)
    plt.show()

This produces the following.

The code above doesn’t specify how finely to divide the interval [0, 1], and by default linspace will produce 50 evenly spaced points. In the example above we need a lot more points. But how many? This is not a simple question.

You could try more points, adding a third argument to linspace, say 1000. That will produce something that looks more like the first plot, but is the first plot right?

The first plot was produced by the Mathematica statements

    f[x_, n_] := Sum[Abs[Sin[Pi k x]/k], {k, 1, n}]
    Plot[f[x, 30], {x, 0, 1}]

Mathematica adaptively determines how many plotting points to use. It tries to detect when a function is changing rapidly in some region and allocate more points there. The function that is the subject of this post makes a good test case for such automated refinement.

Often Mathematica does a good enough job automatically, but sometimes it needs help. The Plot function takes additional parameters like PlotPoints and MaxRecursion that you can use to tell Mathematica to work harder.

It would be an interesting exercise to make a high quality plot of fn for some moderately large n, making sure you’ve captured all the local minima.

[1] Stefan Steinerberger. An Amusing Sequence of Functions. Mathematics Magazine, Vol. 91, No. 4 (October 2018), pp. 262–266

Interview with Sacha Chua

I spoke with Sacha Chua last week. We talked about entrepreneurship, Emacs, having eclectic interests, delegation, and more.

Navigation cons from Sacha's blog

J: I ran into you by searching on Emacs topics. When I look at your blog, I see that you do a lot of interesting things, but it’s a little hard to get a handle on exactly what you do.

S: Oh, the dreaded networking quirky question. What exactly do you do?

J: Yeah, people have said the same thing to me. Not to put you in a box, but I was curious. I see from your site that you do graphic art—sketching and such—and it doesn’t create the impression that you’re someone who would spend a lot of time in front of Emacs. So I’m curious how these things fit together, how you got started using Emacs and how you use it now.

Image by Sacha Chua

S: So my background is actually fairly technical. I’ve been doing computer programming for ages and ages. In high school I came across a book Unix Power Tools, which is how I got interested in Emacs. And because I was interested in programming, in open source, a little bit of wearable computing as well, I got to know Emacs and all these different modules it had. For example, Emacspeak is amazing! It’s been around since the 1990s and it’s a great way to use the computer while you’re walking around. Because I love programming and because I wanted to find a way to help out, I ended up maintaining PlannerMode and later EmacsWiki mode as well.

When I went to university, I took up computer science. After I finished, I taught. Then I took my masters in Toronto, where I am now. Emacs was super helpfu;—being able to do everything in one place. After I finished my masters, I did a lot of software consulting with IBM. I did business consulting as well. Then in 2012, after saving up, I decided to go on pretty much the same adventure you’re on. I’m completely unhirable for the next five years! Most businesses struggle for the first five years, so I saved up enough to not worry too much about my expenses for the next five years. I’m one year in, four years to go, and that’s where I am.

At networking events, I like to shake people up a bit by telling them I’m semi-retired. I’m in this five-year experiment to see how awesome life can be and what I can do to make things better. I’ve done technical consulting, business consulting, sketching, illustration, writing, all sorts of things. Basically, my job description is context-dependent.

J: I understand that.

S: I use Emacs across all the things I do. When I’m doing technical and business consulting, I use Emacs to edit code, to draft documents, even to outline comic strips. And when I’m doing illustration, Emacs—especially Org Mode—helps me keep track of clients and deliverables, things to do, agenda, calendar, deadlines.

J: I’m basically running my life through Org Mode right now. When you say you use Emacs to draft documents, are you using LaTeX?

S: I used LaTeX when I was working on my master’s thesis and other papers, I think. Now I mostly use org mode and export from there.

J: Are you using Emacs for email?

S: I used to. But I’m stuck on Windows to use drawing programs like Sketchbook Pro on my Tablet PC. So it’s harder to set up my email like I had it set up when I used Ubuntu. Back when I used Ubuntu, I was very happy with Gnus.

J: Do you work entirely on Windows, or do you go back and forth between operating systems?

S: I have a private server that runs Linux. On Windows I run Cygwin, but I miss some of the conveniences I had when I had a nicely set-up Linux installation.

J: When you’re running Emacs on Windows, I’m sure you run into things that don’t quite work. What do you do about that?

S: Most things work OK if they’re just Emacs Lisp, but some things call a shell command or use some library that hasn’t been ported over yet. Then I basically wail and gnash my teeth. Sometimes I get things working by using Cygwin, but sometimes it’s a bit of a mess. I don’t use Emacs under Cygwin because I prefer how it works natively. I don’t run into much that doesn’t work.

J: So what programming languages do you use when you’re writing code?

S: I do a lot of quick-and-dirty things in Emacs Lisp. When I need to do some XML parsing or web development, I’ll use Ruby because a lot of people can read it and there are a lot of useful gems. Sometimes I’ll do some miscellaneous things in Perl.

I love doing programming and putting together tools. And I quite enjoy drawing, helping people with presentation and design. So this is left brain plus right brain.

It does boggle people that you can have more than one passion, but others are, like, “Yeah, I know, I’m like that too.”

J: I think having an interest in multiple things is a healthier lifestyle, but it’s a little harder to market.

S: Actually, no. I finally figured out a name for my company, ExperiVis, after a year of playing with it. People reach out to me and we figure out whether it’s a good fit. I don’t need to necessarily guide people to just this aspect or another of my work. I like the fact that people bump into these different things.

J: When we scheduled this call, I went through your virtual assistant. How do you use a virtual assistant?

S: One of the things I don’t like to do is scheduling. I used to get stressed out about scheduling when I did it myself. I’ve always been interested in delegating and taking advantage of what other people enjoy and are good at. I work with an assistant—Criselda. She lives in the Philippines. I found her on oDesk. She works one to four hours a week, more or less, and keeps track of her time.

J: What else might you ask a VA to do?

S: I’ve asked people to do web research. I’ve had someone do a little bit of illustration for me. I’ve had someone do a little bit of programming for me because I want to learn how to delegate technical tasks. He does some Rails prototyping for me. I have someone doing data entry and transcription. It’s fascinating to see how you can swap money for time, especially for things that stress me out, or bore me, or things I can’t do.

Every week I go over my task list with my VA to see which of the tasks I should have delegated. Still working on it!

* * *

Later on in the conversation Sacha asked about my new career and had this gem of advice:

Treating this as a grand experiment makes it much easier for me to try different approaches and not be so scared, to not treat it as a personal rejection if something doesn’t work.

Related post: People I’ve interviewed and people who have interviewed me

R without Hadley Wickham

Tim Hopper asked on Twitter today:

#rstats programming without @hadleywickham’s libraries is like ________ without _________.

Some of the replies were:

  • (skydiving, a parachute)
  • (gouging your own eyes out, NULL)
  • (dentistry, anesthesia)
  • (shaving, a razor)
  • (Internet shopping, credit card)

Clearly there’s a lot of love out there for Hadley Wickham’s R packages. I’m familiar with his ggplot2 graphics package, and it’s quite impressive. I need to look at his other packages as well.

Interview with Michael Hammel, author of The Artist’s Guide to GIMP

Michael J. Hammel is the author of The Artist’s Guide to GIMP, a book I reviewed three weeks ago. The following interview is based on my correspondence with Michael.

 

JC: The bio on your book says that you are an embedded software developer. Is GIMP something you use for fun or is it related to your work? Was it related to your work before?

MH: I use it once in awhile at work but not often. GIMP started as something related to what I worked on when it first came out (graphics software for Unix systems). But I’ve never been paid to be an artist. At one point I looked at trying to use what I knew about it to leverage my way into Pixar or Dreamworks. But that never panned out and my interest in that kind of animation has peeked and ebbed, mostly because I decided I didn’t want to live in California (too expensive).

JC: How did you become involved in the development of GIMP?

MH: When GIMP was born, back around 1995 or so, I was working for a company that provided commercial X servers for Unix systems (Xi Graphics, started by Thomas Roell who wrote the original reference implementation of an X server for 386 systems, aka X386, which begot XFree86 which begot X.org). I packaged Motif as part of my work. A guy I worked with noticed GIMP’s Motif version and pointed me at it. I wrote a plugin for John Beale’s sparkle code (the original Sparkle plugin). I’ve been working with GIMP ever since.

JC: Are you still a developer for GIMP or primarily a user?

MH: A user. I haven’t provided any useful code development to the project in quite sometime.

JC: What’s it like to be a developer on GIMP?

MH: Take this with a grain of salt. It’s a follower’s view from the outside, without being involved with the developers directly.

GIMP’s developer community is somewhat different than other open source communities that I’ve been involved with. GIMP’s leadership is de facto, but not necessarily structured. Many other projects have defined leaders with defined tasks. GIMP’s development is a little more oriented towards scratching your own itch: pick something that that you want to improve and start working on it. The only tough part (at least for some) is understanding and adhering to the main developers coding styles and rules, which are not (to my knowledge) written down.

My only complaint with this style is it tends to allow long term development cycles. End users can’t rely on when new features might be available. While the developers want do things “the right way”, that means that users have been waiting a very long time, for example, for deep paint support. There is a tradeoff for how you do development. Remember that the developers are volunteers, so there is no valid reason for complaining if you, as a user, aren’t getting what you want when you want it. If it bothers you, step in and help out. Otherwise, do as I do: be content with their development process.

JC: What advice would you give people who want to learn image editing?

MH: I learned how to use GIMP by reading Photoshop texts. Image editing is a common set of processes. Think about what you want to do if you weren’t using software. You’re house needs a new color? You mask off the window boarders and roof line and start painting. Same thing in image editing. Only difference: it’s easier to try different colors. Image editing is all about selections and masks. I’m not talking about creating the next Marvel comic, I mean editing existing images. Once you identify what needs to be changed using a selection and mask, you can do all kinds of things: copy and distort, recolor, refocus, etc.

Making comics or custom drawings requires real artistic talent to create a rough design on paper, then import (via scanner or digital camera) into GIMP for cleanup and coloring.

Personally, I prefer taking photos and mixing them together to create a scene that otherwise wouldn’t exist. There is a small example of this in the book. A photo of a city skyline as it exists and a munged version I created to make the city look decayed and falling apart. Beats me what people think of that image, but the creative process that went into it was very relaxing.

JC: What advice would you give people who want to learn GIMP in particular?

MH: Buy my book. Seriously. You learn both at the same time: what is image editing and how do you do it with GIMP. GIMP is just like any other desktop software. You learn where the menus are and what the various windows provide. The rest of your time is learning process — creating a workflow to produce a specific image type or style.

JC: What other software do you use/recommend for creating graphics?

MH: I use simple viewers like Geeqie for browsing images, but if you’re into photography you’ll nee something like f-spot. Photographers, who are nuts about image quality, should learn ufraw. Those interested in 3D or animation should take a look at Blender.

JC: Do you want to say anything about your work with embedded development?

MH: It’s my primary focus in life these days. I’m working on creating a custom media-focused Linux distribution (which I call BeagleBox) for the BeagleBoard C4 (and later the xm). I’d like to get my hands on the Raspberry Pi but wasn’t able to place an order when they first went on sale.

Embedded work is the future for Linux. The desktop for non-techies might change to tablets or such, but Linux will be in everything: your fridge, your TV, your car, your electrical system, your water and sewage systems, your phone, you’re neighbors who are flying robotic UAVs to peek in your windows. Everything.

My day job is building a radar system for UAVs (the big ones) that use embedded Linux to run out of flash. But the direction of interest is smaller devices, little boards with processors and memory in one, like the Pi or BeagleBoard, that can sense the environment and communicate to the rest of the household, with one of those devices used to display all the info on a big wall, removing the need for specialized devices just for the purpose of display.

Anyway, I’m really into making my own custom distribution based on Crosstool-NG (for cross compiling), u-boot (bootloader), Buildroot/Busybox (for root filesystems) and of course Linux.

The Artist’s Guide to GIMP

Learning how to edit images has been on my to-do list for a long time. I do some very basic editing with Paint.NET, but I keep meaning to learn to use GIMP.

I heard years ago that GIMP is terribly complicated and that I should learn Photoshop instead. I tried Photoshop—not extensively, but I spent some time with it—and I tried GIMP. My conclusion was that image editing is complicated. Image editing software is complicated, but not unnecessarily so. If you expect image processing to be as easy as word processing, as I suppose I implicitly did, your expectations are unrealistic.

The only way I’m going to learn image editing is by doing it. But a book can help, and I expect The Artist’s Guide to GIMP will help more than other GIMP resources I’ve read. That’s because this book puts a little more emphasis on problem solving strategies and techniques and a little less emphasis on software features. As Michael Hammel says in the introduction,

This is a book about process, not buttons or menu paths. … Don’t get bogged down in the mechanics of the tool. Focus on the task at hand. I’ll point you to the GIMP components necessary to finish the job.

The first chapter is about tools more than process, but I suppose some of that is inevitable at first. The rest of the book really is more about process. And there’s plenty of information about buttons and menu paths, but the book is organized by task and process comes before tool details. This is what I want from a book. I can read online documentation, but that documentation won’t tell me what I ought to be looking up. I look to books to get me started in the right direction.

Thoughts on the new Windows logo

I appreciate spare design, but the new Windows logo is just boring.

Here’s the rationale for the new logo according to The Windows Blog:

But if you look back to the origins of the logo you see that it really was meant to be a window. “Windows” really is a beautiful metaphor for computing and with the new logo we wanted to celebrate the idea of a window, in perspective. Microsoft and Windows are all about putting technology in people’s hands to empower them to find their own perspectives. And that is what the new logo was meant to be. We did less of a re-design and more to return it to its original meaning and bringing Windows back to its roots – reimagining the Windows logo as just that – a window.

Greg Hewgill had a different perspective:

If you think about it, the new logo sort of looks like deck chairs on the Titanic when it stern was up in the air…

Including LaTeX in an Inkscape drawing

My previous post described how to include an Inkscape drawing in a LaTeX document. This post describes how to use LaTeX in an Inkscape drawing, which is probably more useful. The LaTeX output is included not as bitmap but as a vector drawing that can then be manipulated with all the features of Inkscape.

labeled triangle

The Inkscape book describes the InkLaTeX extension, but the website for InkLaTeX recommends a newer extension textext. Once textext is installed, you can insert LaTeX into an Inkscape drawing by going to the Extensions menu and selecting “TeX Text”. This launches a window in which to type your LaTeX source.

Before I could install textext, I had to install pstoedit. The textext extension also requires LaTeX and Ghostscript, but these were already on my computer. pstoedit has several installation options; I chose the default basic option and that worked. Also, pstoedit says that it requires two Visual C++ runtime DLLs: msvcr70.dll and msvcp70.dll. I already had these, but the pstoedit site gives a link to where you can find these DLLs if you need them.

I had Inkscape running when installed textext and I had to restart Inkscape to see the “TeX Text” menu.

Related post: Including an Inkscape drawing in LaTeX

Including an Inkscape drawing in LaTeX

The Inkscape drawing package can export to a large variety of vector drawing formats, including LaTeX. If you save your drawing to a file foo.tex, you can include the file in a LaTeX document as follows.

\documentclass{article}
\usepackage{pstricks}
\begin{document}
Testing Inkscape \LaTeX\ output.

\input{foo.tex}

\end{document}

Of course you could always export the drawing to an image format and include that image the way you’d include any other image. But you also have the option of directly including the content Inkscape output in your LaTeX file rather than referencing it as an external file using the input statement. This makes your LaTeX file self-contained and is something you could not do, for example, with a PNG file.

Two notes:

  1. You must use the pstricks package.
  2. You must compile the file with latex and not pdflatex. To create a PDF file, you must first compile to PostScript.

The next post is a sort of opposite of this one. It explains how to use LaTeX inside an Inkscape drawing.

Function plots in Inkscape

Why would you want to plot a mathematical function using a drawing package like Inkscape rather than a mathematical package like Mathematica or R? One reason is that you may want plot for its visual properties. For example, you might want to include a sine wave in a drawing.

Another reason is that you may want to have more control (or at least easier control) over your plot. Mathematical packages make it easy to produce a basic plot with default options. But I’ve found it difficult to change the aesthetics of a plot in every mathematical package I’ve used. The things I want to do are often possible but require arcane options that I have trouble remembering. In a drawing program, it’s obvious how to manipulate a plot as an image.

butterfly curve

Inkscape provides a couple extensions to include function plots in a drawing. One is “Function Plotter” and the other is “Parametric Curves.” Both are found under Extensions -> Render. The following dialog shows the settings used to produce the graph above.

The first time I tried using these extensions nothing happened. Then I discovered you have to select a rectangle to contain the plot before creating a plot; the plotting tools do not create their own rectangles.

The Function Plotter supports rectangular and polar coordinates. You’re in for quite a surprise if you expect rectangular coordinates when the polar coordinates box is checked.

Bezier basics

Bézier curves are very common in computer graphics. They also interesting mathematical properties. This post will give a quick introduction to Bézier curves, describing them first in visual terms and then in mathematical terms.

There are different degrees of Bézier curves: linear, quadratic, cubic, etc. Linear Bézier curves are just straight lines. The most common kind of Bézier curve in drawing programs is the cubic and that’s the one I’ll describe below.

Bezier curve

A cubic Bézier curve is determined by four points: two points determine where the curve begins and ends, and two more points determine the shape. Say the points are labeled P0, P1, P2, and P3. The curve begins at P0 and initially goes in the direction of P1. It ends at P3 going in the direction of a line connecting P2 and P3. If you move P1 further away from P0, the curve flattens, going further in the direction of P1 before turning. Similar remarks hold for moving P2 away from P3.

Now for equations. The cubic Bézier curve is given by

B(t) = (1 − t)3 P0 + 3(1 − t)2t P1 + 3(1 − t)t2 P2 + t3 P3

for t running between 0 and 1. It’s clear from the equation that B(0) = P0 and B(1) = P3. A little calculation shows that the derivatives satisfy

B‘(0) = 3(P0P1)

and

B‘(1) = 3(P3P2).

Moving the points P1 and P2 further out increases the derivatives and thus makes the curve go further in the direction of these points before bending.

Related post: The smoothest line through a set of points