Help / Learning / Ch 20: Scalar Numerical Functions

From J Wiki
Jump to navigation Jump to search


>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Learning J



Chapter 20: Scalar Numerical Functions

In this chapter we look at built-in scalar functions for computing numbers from numbers. This chapter is a straight catalog of functions, with links to the sections as follows:

Ceiling Conjugate cos cos<sup>-1</sup> cosh cosh<sup>-1</sup>
Decrement divide Double Exponential Factorial Floor
GCD Halve Increment LCM Logarithm Log, Natural
Magnitude Minus multiply Negate OutOf PiTimes
Plus power Pythagorean Reciprocal Residue Root
Signum sin sin<sup>-1</sup> sinh sinh<sup>-1</sup> Square
SquareRoot tan tan<sup>-1</sup> tanh tanh<sup>-1</sup>  

20.1 Plus and Conjugate

Dyadic + is arithmetic addition.

2 + 2 1.5 + 0.25 3j4 + 5j4 2r3 + 1r6
4 1.75 8j8 5r6

Monadic + is "Conjugate". For a real number y, the conjugate is y. For a complex number xjy (that is, x + 0jy), the conjugate is x - 0jy.

+ 2 + 3j4
2 3j_4

20.2 Minus and Negate

Dyadic - is arithmetic subtraction.

2 - 2 1.5 - 0.25 3 - 0j4 2r3 - 1r6
0 1.25 3j_4 1r2

Monadic - is "Negate".

- 2 - 3j4
_2 _3j_4

20.3 Increment and Decrement

Monadic >: is called "Increment". It adds 1 to its argument.

>: 2 >: 2.5 >: 2r3 >: 2j3
3 3.5 5r3 3j3

Monadic <: is called "Decrement". It subtracts 1 from its argument.

<: 3 <: 2.5 <: 2r3 <: 2j3
2 1.5 _1r3 1j3

20.4 Times and Signum

Dyadic * is multiplication.

2 * 3 1.5 * 0.25 3j1 * 2j2 2r3 * 7r11
6 0.375 4j8 14r33

Monadic * is called "Signum". For a real number y, the value of (* y) is _1 or 0 or 1 as y is negative, zero or positive.

* _2 * 0 * 2
_1 0 1

More generally, y may be real or complex, and the signum is equivalent to y % | y. Hence the signum of a complex number has magnitude 1 and the same angle as the argument.

y =: 3j4 | y y % | y * y | * y
3j4 5 0.6j0.8 0.6j0.8 1

20.5 Division and Reciprocal

Dyadic % is division.

2 % 3 1.4 % 0.25 3j4 % 2j1 12x % 5x
0.666667 5.6 2j1 12r5

1 % 0 is "infinity" but 0 % 0 is 0

1 % 0 0 % 0
_ 0

Monadic % is the "reciprocal" function.

% 2 % 0j1
0.5 0j_1

20.6 Double and Halve

Monadic +: is the "double" verb.

+: 2.5 +: 3j4 +: 3x
5 6j8 6

Monadic -: is the "halve" verb:

-: 6 -: 6.5 -: 3j4 -: 3x
3 3.25 1.5j2 3r2

20.7 Floor and Ceiling

Monadic <. (left-angle-bracket dot) is called "Floor". For real y the floor of y is y rounded downwards to an integer, that is, the largest integer not exceeding y.

<. 2 <. 3.2 <. _3.2 <. 4r3
2 3 _4 1

For complex y, the floor lies within a unit circle center y, that is, the magnitude of (y - <. y) is less than 1.

y =: 3.4j3.4 z =: <. y y - z | y-z
3.4j3.4 3j3 0.4j0.4 0.565685

This condition (magnitude less than 1) means that the floor of say 3.8j3.8 is not 3j3 but 4j3 because 3j3 does not satisfy the condition.

y =: 3.8j3.8 z =: <. y | y-z | y - 3j3
3.8j3.8 4j3 0.824621 1.13137

Monadic >. is called "Ceiling". For real y the ceiling of y is y rounded upwards to an integer, that is, the smallest integer greater than or equal to y. For example:

>. 3.0 >. 3.1 >. _2.5
3 4 _2

Ceiling applies to complex y

>. 3.4j3.4 >. 3.8j3.8
3j4 4j4

20.8 Power and Exponential

Dyadic ^ is the "power" verb: (x^y) is x raised-to-the-power y

10 ^ 2 10 ^ _2 100 ^ 1%2
100 0.01 10

Monadic ^ is exponentiation (or antilogarithm): ^y means (e^y) where e is Euler's number, 2.71828...

^ 1 ^ 1.5 ^ 3r2 ^ 0j1
2.71828 4.48169 4.48169 0.540302j0.841471

Euler's equation, supposedly engraved on his tombstone is:

e  i π +1 = 0 
   (^ 0j1p1) + 1
0j1.22465e_16

The example of ^ 3r2 above shows that rationals are in the domain of ^ , but the result is real, not rational. A rational argument is in effect first converted to real.

20.9 Square

Monadic *: is "Square".

*: 4 *: 2j1
16 3j4

20.10 Square Root

Monadic %: is "Square Root".

%: 9 %: 3j4 2j1 * 2j1
3 2j1 3j4

20.11 Root

If x is integral, then x %: y is the "x'th root" of y:

3 %: 8 _3 %: 8
2 0.5

More generally, (x %: y) is an abbreviation for

(y ^ % x)
x =: 3 3.1 x %: 8 8 ^ % x
3 3.1 2 1.95578 2 1.95578

20.12 Logarithm and Natural Logarithm

Dyadic ^. is the base-x logarithm function, that is,

(x ^. y) is the logarithm of y to base x :
10 ^. 1000 2 ^. 8 10 ^. 2j3 10 ^. 2r3
3 3 0.556972j0.426822 _0.176091

Monadic ^. is the "natural logarithm" function.

e =: ^ 1 ^. e ^. 2j3 ^. 2r3
2.71828 1 1.28247j0.982794 _0.405465

The example of ^. 2r3 above shows that rationals are in the domain of ^. but the result is real, not rational. A rational argument is in effect first converted to real.

20.13 Factorial and OutOf

The factorial function is monadic !.

! 0 1 2 3 4 ! 5x 6x 7x 8x
1 1 2 6 24 120 720 5040 40320

The number of combinations of x objects selected out of y objects is given by the expression x ! y

1 ! 4 2 ! 4 3 ! 4
4 6 4

20.14 Magnitude and Residue

Monadic | is called "Magnitude". For a real number y the magnitude of y is the absolute value:

| 2 | _2
2 2

More generally, y may be real or complex, and the magnitude is equivalent to (%: y * + y).

y =: 3j4 y * + y %: y * + y | y
3j4 25 5 5

The dyadic verb | is called "Residue". the remainder when y is divided by x is given by (x | y).

10 | 12 3 | _2 _1 0 1 2 3 4 5 1.5 | 3.7
2 1 2 0 1 2 0 1 2 0.7

If x | y is zero, then x is a divisor of y:

4 | 12 12 % 4
0 3

The "Residue" function applies to complex numbers:

a =: 1j2 b=: 2j3 a | b a | (a*b) (b-1j1) % a
1j2 2j3 0j_1 0 1

20.15 GCD and LCM

The greatest common divisor (GCD) of x and y is given by (x +. y). Reals and rationals are in the domain of +..

6 +. 15 _6 +. _15 2.5 +. 3.5 6r7 +. 15r7
3 3 0.5 3r7

Complex numbers are also in the domain of +..

a=: 1j2 b=:2j3 c=:3j5 (a*b) +. (b*c)
1j2 2j3 3j5 2j3

The Least Common Multiple of x and y is given by (x *. y).

(2 * 3) *. (3 * 5) 2*3*5
30 30

20.16 Pi Times

There is a built-in verb o. (lower-case o dot). Monadic o. is called "Pi Times"; it multiplies its argument by 3.14159...

o. 1 o. 2 o. 1r6 o. 2j3
3.14159 6.28319 0.523599 6.28319j9.42478

The example of o. 1r6 above shows that rationals are in the domain of sin but the result is real, not rational. A rational argument is in effect first converted to real.
A result with arbitrary precision can be be produced by giving an argument of an extended integer to the verb <. @ o. for which there is special code in the J interpreter

   ] z =: (<. & o.) 10^40x
31415926535897932384626433832795028841971
   datatype z
extended
   
   

20.17 Trigonometric and Other Functions

If y is an angle in radians, then the sine of y is given by the expression 1 o. y. The sine of (π over 6) is 0.5

y =: o. 1r6 1 o. y
0.523599 0.5

The general scheme for dyadic o. is that (k o. y) means: apply to y a function selected by k. Here y is an angle in radians.
Giving conventional names to the available functions, we have:


   sin   =:  1 & o.  NB.  sine
   cos   =:  2 & o.  NB.  cosine 
   tan   =:  3 & o.  NB.  tangent
   
   sinh  =:  5 & o.  NB.  hyperbolic sine 
   cosh  =:  6 & o.  NB.  hyperbolic cosine  
   tanh  =:  7 & o.  NB.  hyperbolic tangent 
   
   asin  =: _1 & o.  NB.  inverse sine 
   acos  =: _2 & o.  NB.  inverse cosine 
   atan  =: _3 & o.  NB.  inverse tangent 
   
   asinh =: _5 & o.  NB.  inverse hyperbolic sine
   acosh =: _6 & o.  NB.  inverse hyperbolic cosine 
   atanh =: _7 & o.  NB.  inverse hyperbolic tangent
   
   

y sin y asin sin y sin 1r4
0.523599 0.5 0.523599 0.247404

The example of sin 1r4 above shows that rationals are in the domain of sin but the result is real, not rational. A rational argument is in effect first converted to real.

20.18 Pythagorean Functions

There are also the "pythagorean"functions:

          0 o. y  means   %:   1 - y^2
          4 o. y  means   %:   1 + y^2
          8 o. y  means   %: - 1 + y^2
         _4 o. y  means   %:  _1 + y^2
         _8 o. y  means - %: - 1 + y^2 

y =: 0.6 0 o. y  %: 1 - y^2
0.6 0.8 0.8

and a further group of functions on complex numbers:

          9 o. xjy   means  x                (real part)
         10 o. xjy   means  %: (x^2) + (y^2) (magnitude) 
         11 o. xjy   means  y                (imag part)
         12 o. xjy   means  atan (y % x)     (angle)

9 o. 3j4 10 o. 3j4 11 o. 3j4 12 o. 3j4
3 5 4 0.927295

and finally

          _9 o. xjy   means xjy                 (identity)
         _10 o. xjy   means x j -y              (conjugate)
         _11 o. xjy   means 0j1 * xjy           (j. xjy)
         _12 o. a     means (cos a)+(j. sin a)  (inverse angle)

For example:

a =: 12 o. 3j4 _12 o. a
0.927295 0.6j0.8

This is the end of chapter 20



NEXT
Table of Contents

Index



The examples in this chapter were executed using J version 701.

This chapter last updated 05 Oct 2012

Copyright © Roger Stokes 2012.

This material may be freely reproduced, 

provided that this copyright notice is also reproduced.


>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Learning J