Article 5E2CQ Trig functions across programming languages

Trig functions across programming languages

by
John
from John D. Cook on (#5E2CQ)

Programming languages are inconsistent in their support for trig functions, and inconsistent in the names they use for the functions they support. Several times I've been irritated by this and said that I should make a comparison chart someday, and today I finally did it.

Here's the chart. The C column also stands for languages like Python that follow C's conventions. More on this below.

trig_comp.png

The table above is a PNG image. An HTML version of the same table is available here.

The rest of the post discusses details and patterns in the table.

Introduction

There are six trig functions according to the most common convention: sine, cosine, tangent, secant, cosecant, and cotangent. Your list could be longer or shorter, depending on how you count them. These six functions have inverses (over some range) and so a math library could have 12 trig functions, including inverses.

Except there's a wrinkle with the inverse tangent. For any given t there are infinitely many angles whose tangent is t. Which one do you want? By convention, we usually want between -/2 and /2. But it's handy sometime to specify a point (x, y) and ask for the angle made by the line from the origin to the point. In that case we a value of in the same quadrant as the point. So if y/x = z, the one-argument form of inverse tangent depends only on z, but the two-argument depends on both x and y; two points with equal ratios may result in different choices of .

So with our two versions on inverse tangent we have a total of 13 functions. This post will explain which of these 13 functions are supported in C, Python, R, Perl, Mathematica, bc, and Common Lisp, and how the supported functions are named.

Mathematica

Of the languages I looked at, only Mathematica implements all 13 functions. The names of the six basic functions are what you'd see in a contemporary calculus textbook except that, like all functions in Mathematica, names begin with a capital letter.

So the six trig functions are Sin, Cos, Tan, Sec, Csc, and Cot. The inverse functions are the same with an Arc prefix: ArcSin, ArcCos, etc.

The two inverse tangent functions are both named ArcTan. With one argument, ArcTan[z] returns an angle between -/2 and /2 such that tan = z. With two arguments, ArcTan[x, y] returns an angle in the same quadrant as (x, y) with tan = y/x.

C, Python, and R

Python and R follow C's lead, as do other programming languages such as JavaScript.

C does not support sec, csc, and cot, presumably because they're simply the reciprocals of cos, sin, and tan respectively. It does not support their inverses either.

Inverse trig functions are denoted with an a prefix: asin, acos, atan. The two-argument form of inverse tangent is atan2. The order of arguments to atan2 differs from that of Mathematica and is discussed in the section on Common Lisp below.

NumPy

NumPy supports the same trig functions as base Python, but it uses different names for inverse functions. That is, NumPy uses arcsin, arccos, arctan, and arctan2 while Python's standard math module uses asin, acos, atan, and atan2.

Common Lisp

Common Lisp is the same as C except for the inverse tangent function of two arguments. There CL is similar to Mathematica in that the same name is used for the function of one argument and the function of two arguments.

However, the order of the arguments is reversed in CL relative to Mathematica. That is, (atan y x) in CL equals ArcTan[x, y] in Mathematica.

Even though the two languages use opposite conventions, there are good reason for both. Mathematica interprets the arguments of ArcTan[x, y] as the coordinates of a point, written in the usual order. Common Lisp interprets (atan y x) as a function whose second argument defaults to 1.

Mathematica has the advantage that the coordinates of a point are listed in the natural order. Common Lisp has the advantage that the meaning of the first argument does not change if you add a second argument.

C and languages like Python and R that follow C use the same convention as Common Lisp, i.e. the first argument to atan2 is the second coordinate of a point.

Perl

The base Perl language only supports three trig functions: sine, cosine, and two-argument inverse tangent. The module Math::Trig supports everything else, including the reciprocal functions and their inverses.

It's interesting that Perl made the choices that it did. There are languages, like bc discussed below, that support inverse tangent of with one argument but not two; Perl is the only language I know of that does the opposite, supporting inverse tangent with two arguments but not one. It makes sense that if you're only going to support one, you support the more general of the two.

Perl's atan2 function uses the same argument convention as C et al., i.e. atan2(y, x), numerator first.

In the Math::Trig module, the reciprocal trig functions are named sec, csc, and cot, as is standard now. (You may see things like cosec and ctn in older math books.) Inverse functions have an a prefix, as they do in C.

There's one inexplicable quirk in Math::Trig: the functions sin and cos aren't there, but atan2 is. I could see leaving out sin and cos because they're redundant with base Perl, but so is atan2. It would be more consistent to add sin and cos (my preference) or take out atan2.

bc

The Unix calculator bc has a minimal set of trig functions: sine, cosine, and (one-argument) inverse tangent. These are denoted simply s, c, and a. But you can bootstrap your way from these three functions to all the rest.

The post Trig functions across programming languages first appeared on John D. Cook.GRtqAT8qAEI
External Content
Source RSS or Atom Feed
Feed Location http://feeds.feedburner.com/TheEndeavour?format=xml
Feed Title John D. Cook
Feed Link https://www.johndcook.com/blog
Reply 0 comments