[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: shell wrappers for trig and other mathematical functions



On Tue 01 Oct 2019 at 21:18:13 (-0700), Dan Hitt wrote:
> On Tue, Oct 1, 2019 at 8:58 PM David Christensen <dpchrist@holgerdanske.com> wrote:
> > On 10/1/19 8:32 PM, Dan Hitt wrote:
> > > I'm half-way looking for some shell wrappers for common trig functions like
> > > sin, cos, exp, log, and others.
> > >
> > > I'm aware of bc, but it seems cumbersome.
> > >
> > > I would like to just type 'sin 1' and get the sine (of 1 radian),
> >
> > Perl one-liners are an option:
> >
> > 2019-10-01 19:25:59 dpchrist@tinkywinky ~
> > $ perl -e 'print sin 1'
> > 0.841470984807897
> >
> >
> > > or type 'log 2'
> >
> > 2019-10-01 20:48:52 dpchrist@tinkywinky ~
> > $ perl -e 'print log 2'
> > 0.693147180559945
> >
> >
> > > and get the natural or maybe common log of 2.  (Probably any such
> > > program should do something intelligent when faced with multiple or zero
> > > arguments, such as computing the sine or log of each, so that they could be
> > > chained together.  And maybe such a program would pay attention to
> > > environment variables or optional command line arguments to tune its
> > > behavior.)
> > >
> > > These kinds of programs would be super-easy to write in just about any
> > > language (i guess perhaps even just as bash functions which shell out to bc
> > > for at least some of the simpler functions) but before i actually do
> > > something like that, i wonder if somebody has already done it, whether
> > > there exist any standards or good ideas, etc.  (Because if somebody has a
> > > good, thoughtful exp program, for example, then it could be cookie-cutter
> > > copied to a bunch of other functions.)
> > >
> > > There is a precedent of sorts in Paul Rubin's factor program, which is just
> > > oh-so-handy when you're wondering how an integer factors, but don't want to
> > > start up some heavy-weight system just to find out.
> >
> > 2019-10-01 20:49:17 dpchrist@tinkywinky ~
> > $ apt-cache search libmath-prime-util-perl
> > libmath-prime-util-perl - utilities related to prime numbers, including
> > fast sieves and factoring
> Thanks so much for your reply, including the apt-cache searching part.
> 
> It does look like a way to quickly get values for sine (or any other
> function in perl).
> 
> However, i would like to dispense entirely with the 'perl -e' and 'print'
> part.  I really would like stand-alone programs.  This would absolutely
> minimize any typing, and if the programs had a set of good conventions,
> then they would provide a model for writing others if the functions i want
> are not already available in perl (or other interpreter).

You mean like:

$ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import *
>>> sin(1)
0.8414709848078965
>>> log(2)
0.6931471805599453
>>> log10(2)
0.3010299956639812
>>> exp(2)
7.38905609893065
>>> sqrt(10)
3.1622776601683795
>>> factorial(9)
362880
>>> import math
>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>> 

By importing math with "from math import *", the functions are made
available without having to be qualified: "factorial()" rather than
"math.factorial()". I imported math a second time just to give you
the contents of that particular module with dir().

Cheers,
David.


Reply to: