Complex variable functions principle and
non-principle solution
ABCD
update 2010-10-24
This is a real number calculator, complex number calculator.
You need to know javascript commands to use this file.
This file is written under MSIE 6.0
Document
Compare
complex number online reference
01
02
03
04
05
This file is personal home work. Output
may contain error. Please verify first.
This file has complex function source code.
But can not promise they are all correct.
Below has thirty test button on left column.
[complex01] and [complex02] are input boxes.
[complex output] is output for principle value.
One-to-many function output to Box1. Non-principle
value answer no confirmation. You need to verify
first. 9803220737
Proof +1=-1.
if know the mathematics rule leak hole, then one can "proof"
Below is complex function for add/subtract/multiply/division
etc. non-math operation.
;
1
0 =
delete function name, make room for output box
9804130942
Above is complex function for add/subtract/multiply/division etc. non-math operation.
Below is complex function, math operation, many-to-one.
no need to use +2*n*PI
9803161014
complex01=c1 in function name,
complex02=c2 in function name.
dot product, complex separated by
comma or
';' or
blank
dot product use 〈u, v_conj〉, 〈u_conj, v〉 answer conjugate to each other.
cexpf('3i') = cexpi(3) no need to use "i", no need to use quote. 9805181224
cexpf('1+3i')=cexpi('1+3i') ; cexpf(2)=cexpi('2+0i')
Above function all not output to box1 below.
Below function all do output to box1 below.
Below is one-to-many functions, use +2*n*PI
n is arbitrary integer.
9803151835
Function argument is following
c1 and c2 are given (input) complex numbers.
rtsq01 determine square root take '+' (rtsq01!=1)
or take '-' (rtsq01==1)
If omit rtsq01, default take '+' (root01=0)
nBgn,nEnd is n range in 2*n*PI [9803242228]
If omit nBgn,nEnd default n=0 to 0 for principle answer.
If assign nBgn and omit nEnd , return one value for n=nBgn
if assign both nBgn and nEnd , return n=[nBgn,nEnd] solution.
When return value is more than one, programmer need to use
manager function to arrange output for easy reading.
Otherwise ten complex number answer, twenty real/imag
numbers all string together.
This file has manager function, example clogfMan(clArg1)
and all function ...Man()
one-to-many function has +2*n*PI its n range
Box1, answer
Box2, equation doc.
Below has "test box3 command" button, it is in fact
a small calculator. Alert: complex add/subtract
and multiply/division are all carry out by functions
error (1+2*i)+(3+4*i) , correct caddf('1+2*i','3+4*i')
error (1+2*i)-(3+4*i) , correct csubf('1+2*i','3+4*i')
error (1+2*i)*(3+4*i) , correct cmulf('1+2*i','3+4*i')
error (1+2*i)/(3+4*i) , correct cdivf('1+2*i','3+4*i')
(1+2*i) is error, '1+2*i' is correct. 9803161923
Use search engine search for next string
Operator Overloading In Javascript
http://blog.thejit.org/2009/01/27/why-not-operator-overloading-in-javascript/
This page explain why-not-operator-overloading-in-javascript
For complex number c1,c2 we can not use c3=c1+c2
must use c3=caddf(c1,c2).
Box3 default:[sin(1+2i)^2+cos(1+2i)^2]
Box4 should get: 1+0i
click to run
MSIE 6.0
Examples
RUN 19
;
51+ are big
Box3, JS command
/*=*/
stop print;
//^
add blank line
9801022039
Java Script Bible
[[
884 Part IV ✦ JavaScript Core Language Reference
Table 28-1: JavaScript Math Properties
Property Value Description
Math.E 2.718281828459045091 Euler’s constant
Math.LN2 0.6931471805599452862 Natural log of 2
Math.LN10 2.302585092994045901 Natural log of 10
Math.LOG2E 1.442695040888963387 Log base-2 of E
Math.LOG10E 0.4342944819032518167 Log base-10 of E
Math.PI 3.141592653589793116 π
Math.SQRT1_2 0.7071067811865475727 Square root of 0.5
Math.SQRT2 1.414213562373095145 Square root of 2
Because these property expressions return their constant values, you use them in your regular
arithmetic expressions. For example, to obtain the circumference of a circle whose diameter
is in variable d, employ this statement:
circumference = d * Math.PI;
Perhaps the most common mistakes scripters make with these properties are failing to capitalize
the Math object name and observing the case-sensitivity of property names.
Methods
Methods make up the balance of JavaScript Math object powers. With the exception of the
Math.random() method, all Math object methods take one or more values as parameters.
Typical trigonometric methods operate on the single values passed as parameters; others
determine which of the numbers passed along are the highest or lowest of the group. The
Math.random() method takes no parameters but returns a randomized, floating-point value
between 0 and 1 (note that the method does not work on Windows or Macintosh versions of
Navigator 2). Table 28-2 lists all the Math object methods with their syntax and descriptions
of the values they return.
Table 28-2: Math Object Methods
Method Syntax Returns
Math.abs(val) Absolute value of val
Math.acos(val) Arc cosine (in radians) of val
Math.asin(val) Arc sine (in radians) of val
Math.atan(val) Arc tangent (in radians) of val
Math.atan2(val1, val2) Angle of polar coordinates x and y
Math.ceil(val) Next integer greater than or equal to val
Math.cos(val) Cosine of val
Math.exp(val) Euler’s constant to the power of val
Math.floor(val) Next integer less than or equal to val
Math.log(val) Natural logarithm (base e) of val
Math
885 Chapter 28 ✦ The Math, Number, and Boolean Objects
Method Syntax Returns
Math.max(val1, val2) The greater of val1 or val2
Math.min(val1, val2) The lesser of val1 or val2
Math.pow(val1, val2) Val1 to the val2 power
Math.random() Random number between 0 and 1
Math.round(val) N+1 when val >= n.5; otherwise N
Math.sin(val) Sine (in radians) of val
Math.sqrt(val) Square root of val
Math.tan(val) Tangent (in radians) of val
HTML is not exactly a graphic artist’s dream environment, so using trig functions to obtain a
series of values for HTML-generated charting is not a hot JavaScript prospect. Only with the
advent of positionable elements have scripters been able to apply their knowledge of using
these functions to define fancy trajectories for flying elements. For scripters who are not
trained in programming, math is often a major stumbling block. But as you’ve seen so far, you
can accomplish a great deal with JavaScript by using simple arithmetic and a little bit of
logic—leaving the heavy-duty math for those who love it.
Creating random numbers
The Math.random() method returns a floating-point value between 0 and 1. If you design a
script to act like a card game, you need random integers between 1 and 52; for dice, the range
is 1 to 6 per die. To generate a random integer between zero and any top value, use the following
formula:
Math.floor(Math.random() * n)
Here, n is the top number. To generate random numbers between a different range, use this
formula:
Math.floor(Math.random() * (n – m + 1)) + m
Here, m is the lowest possible integer value of the range and n equals the top number of the
range. For the dice game, the formula for each die is
newDieValue = Math.floor(Math.random() * 6) + 1;
Math object shortcut
In Chapter 31, you see details about a JavaScript construction that enables you to simplify
the way you address multiple Math object properties and methods in statements. The trick is
to use the with statement.
In a nutshell, the with statement tells JavaScript that the next group of statements (inside the
braces) refers to a particular object. In the case of the Math object, the basic construction
looks like this:
with (Math) {
//statements
}
Math
886 Part IV ✦ JavaScript Core Language Reference
For all intervening statements, you can omit the specific references to the Math object.
Compare the long reference way of calculating the area of a circle (with a radius of six units)
result = Math.pow(6,2) * Math.PI;
to the shortcut reference way:
with (Math) {
result = pow(6,2) * PI;
}
Though the latter occupies more lines of code, the object references are shorter and more
natural when reading the code. For a longer series of calculations involving Math object properties
and methods, the with construction saves keystrokes and reduces the likelihood of a
case-sensitive mistake with the object name in a reference. You can also include other full object
references within the with construction; JavaScript attempts to attach the object
name only to those references lacking an object name. On the downside, the with construction
is not particularly efficient in JavaScript because it must perform a lot of internal tracking
in order to work.
]]
hyperbolic cosine use
0.5*(exp(x)+exp(-x))
hyperbolic sine use
0.5*(exp(x)-exp(-x))
9507211232
9910180956
sin() cos() tan() etc. use radian
as input, do not accept degree.
Convert degree to radian use next
code line
rad0=deg0*PI/180
or
sin(deg0*PI/180)
On the other hand,
Convert radian to degree use next
code line
deg0=rad0*180/PI
or
var sideRatio=1/2;
rad0=asin(sideRatio)
deg0=rad0*180/PI
9910181007
2009-04-28-11-24
This file complex2.htm has Gamma function
y(t)=gamma(t)
and has step function
y(t) =
-3.097*stepf(t,-2)*(1-stepf(t,2))*sin(t)+stepf(t,2)*(1-stepf(t,5))*exp(-(t-PI))*sin(t-PI)+stepf(t,5)*(1-stepf(t,8))*(t*t*t/108.75-1)
factorial function
y(n)=factorial(n)
step function
onoff=stepf(t0,bgn0)
if t0<bgn0 return value 0
if t0>=bgn0 return value 1
y(t) = -3.097*stepf(t,-2)*(1-stepf(t,2))*sin(t)+stepf(t,2)*(1-stepf(t,5))*exp(-(t-PI))*sin(t-PI)+stepf(t,5)*(1-stepf(t,8))*(t*t*t/108.75-1)
gate function
onoff=gatef(t0,bgn0,end0)
if t0<bgn0 return value 0
if t0>end0 return value 0
else bgn0<=t0<=end0 return value 1
binomial coefficient
value0=bicof(6,2) //do not use binof(6,2)
ISBN 0-12-059820-5 page 307, line-4
(m) m!
( )= ──────
(n) n!*(m-n)!
above is equation defined if m>=n
below is numerical example
(6) 6!
( )= ────── =15
(2) 2!*(6-2)!
double factorial coefficient
value0=facdb(5)
ISBN 0-12-059820-5 page 545, equation 10.33c
odd integer 7!!=1*3*5*7=105
even integer 6!!=2*4*6=48
2009-04-28-12-03 stop
2009-05-20-13-51 done include JSMathFunc to complex2.htm
<a name="doc78">
update 2010-09-21 document
2010-09-21-19-50 start
This file complex2.htm initially
intend to be demonstration for
complex functions. See the page
structure testSet 1, 2, 3.
<a name="doc79">
Wrote code and in/output boxes
to test complex functions. Later
find out the test structure can
be used as real number calculator,
complex number calculator.
complex2.htm use javascript to
find answer. If need the value
of one variable, for example abc,
user put 'abc' in one code line.
<a name="doc80">
complex2.htm convert box3 value
[[
var abc=sin(PI/3)
abc
]]
to
[[
var abc=sin(PI/3)
boxd14.value+="abc" +"\n";
boxd14.value+=abc
boxd14.value+="\n";
]]
<a name="doc81">Examples
run this code, output to box4.
[[
abc
0.8660254037844386
]]
Several version of complex2.htm
remain this method.
<a name="doc82">
Earlier version use the criterion
that in a line, it has '=' or not?
If a line has '=', complex2.htm
do NOT insert [boxd14.value+=]
otherwise, complex2.htm insert.
This simple method is good for
complex2.htm author personal use,
it is awkward for other to use.
<a name="doc83">
For example, code
[[
i=12
if(i<5)
{
i++
}
else
{
i--;
}
i
]]
<a name="doc84">
must write as
[[
i=12
if(i<5) { dummy=0
i++; dummy=0 }
else { dummy=0
i--; dummy=0 }
i
]]
to bring in '='.
<a name="doc85">
update 2010-09-21 has big change
at function evalAll() not rely
upon '=' only. Main point is to
use javascript
var keyword=... //9908190809
complex2.htm check variable name
against keyword list. Now one
code line one 'else' is safe.
User no need to insert dummy=0
any more. But complex2.htm is
<a name="doc86">Examples
not complete compatible with
javascript. For example,
var aa=sin(PI/5);
both complex2.htm & javascript OK
But, same code split to 3 lines
aa
=
sin(PI/5);
javascript OK, complex2.htm error.
<a name="doc87">
Updated complex2.htm can not find
!true
answer. Reason is
!true //WHY not print: no variable
Older version ('=' criterion) give
answer
[[
!true
false
]]
<a name="doc88">
complex2.htm is like a seesaw.
When I see it, complex2.htm saw it.
When I saw it, complex2.htm see it.
Make up and raise one end, the other
end falls. Please do not blame on me,
since this seesaw phenomenon obeys
Heisenberg Uncertainty Principle.
Sigh! //How to go around seesaw?<a name="doc89">
Use javascript code as calculator
author need tell user how to write
code. There are example button from
0 to 15 for small example.
Button 51 (and + in future) is for
bigger example.
Example 51 was chosen from
freeman2.com/tute0050.htm#ch14b054<a name="doc90">
Found that code at ch14b054 was
wrong. 2010-09-20 & 21 made
correction. The new 'corrected'
code (complex2.htm button 51)
still NOT promise to be correct.
Simply no other body proofread,
no other similar program run for
comparison.
2010-09-21-20-56 here
<a name="doc91">Examples
update 2010-09-21 add copyf(arg1)
function. Help user to copy one
array to second (different memory
location) array (not just alias)
Use copyf(arg1) with caution, it
eat out memory quickly if array
is big.
<a name="doc92">
update 2010-09-21 fixed cnewf()
function. Older version must use
as v1=cnewf(1,2) //OK
But v1=cnewf(3) //wrong
and v1=cnewf('4') //wrong
now, all three OK.
Hope you like the calculator I
wrote.
Liu,Hsinhan 2010-09-21-21-09 stop
<a name="doc93">
Next is six reasons of why not print.
You can find them at box5 after run
an example. 2010-09-25-19-25
whyNotPrint=
' has keyword'; //9909201039
' inside if()'; //9909231748
' if \\n ()'; //9909240936
' line has [=]'; //9909201040
' no variable'; //9909201041
' has [++],[--]';//9909221122
'#manyLine1cmd=';//9909291154
' [=] follows '; //9909291539
2010-09-25-17-45 done spelling check
<a name="doc94">Examples
2010-09-28-14-35 start
Now create example 52 code.
Example 52 is a real number, real
value example.
Real number say that it is not
complex number example.
Real value say that it is not a
toy example.
<a name="doc95">
Reference: Quaternions and Rotation
Sequences. ISBN 0-691-05872-5, 1998
by Jack B. Kuipers. (Abbr. as QRS)
<a name="doc96">
Example 52 input is a set of rotated
three dimensional coordinate system.
Output to Eular angles and quaternion.
Before rotation, the coordinate system
has simple three axis vectors, that is
"rotation" matrix be
[1,0,0]
[0,1,0]
[0,0,1]
It is an identity matrix, no rotation
at all.
<a name="doc97">
x0axis is COLUMN 1 [1,0,0] // not row
y0axis is COLUMN 2 [0,1,0] // not row
z0axis is COLUMN 3 [0,0,1] // not row
After rotation, the coordinate system
has user specified three axis vectors,
that is (for example) //9909281442 here
ax=12 degree
ay= 8 degree
az=-6.5 degree
<a name="doc98">Examples
Aerospace Rotation Sequence matrix is
[0.983902,-0.112101,-0.139173]
[0.139479, 0.968584, 0.205888]
[0.111720,-0.221985, 0.968628]
//9909281515 here
What is the meaning of above
rotation matrix? It is simple to
remember.
<a name="doc99">
Before rotation
x0axis is old column 1
[ 1
0
0]
After rotation
x1axis is new column 1
[ 0.983902,
0.139479,
0.111720]
<a name="doc100">
Remind that x axis is a vector.
It has three components in 3-dim.
space.
Similarly,
y1axis is new column 2
[-0.112101,
0.968584,
-0.221985]
<a name="doc101">
z1axis is new column 3
[-0.139173,
0.205888,
0.968628]
//9909281527 here
If you set different initial x,y,z
angles, above vector number should
be different.
//9909281543 here
<a name="doc102">
The following do the reverse. That
is given rotation matrix (like above)
Find Eular angles and quaternion.
From rotation matrix to quaternion
use QRS page 169 four q equations.
//here goto code section. 9909281548
//9909281555 start
Next is from quaternion to Eular
angles. QRS page 168 tan(psi),
sin(theta), tan(phi) equations.
//here goto code section. 9909281558
<a name="doc103">Examples
Upto here, example 52 code is done.
Please goto example section, click
52 button. At top of code, you can
input x:roll, y:pitch, z:yaw angles
in degree. At the end of code, program
back to same numbers which confirm
calculation. In middle, you get
rotation matrix and quaternion.
2010-09-28-16-20 stop
<a name="doc104">
2010-09-28-19-08 start
Quaternion is also named as Euler's
Parameter. For example, Classical
Mechanics second edition, by
Herbert Goldstein, page 153 last
line. Page 153 and 165 have related
formula. LiuHH first time learned
as Euler's parameter at University
of Iowa in Professor Edward Haug's
<a name="doc105">
class. First time see Quaternion
when read Rotations, Quaternions,
and Double Groups. by Simon L.
Altmann ISBN 0-486-44518-6 around
2006-Aug to 2007. After read and
think found that Quaternion and
Euler's Parameter are one thing.
2010-09-28-19-19 stop
<a name="doc106">
update 2010-10-03 //update 2010-10-04
2010-09-29-20-35 start
2010-09-26 rewrite Chinese version
complex1.htm Read code carefully.
Found several error/bug. Change as
following
1)
When search for '//', exclude '://'
Time stamp is 9909271146.
<a name="doc107">
2)
delete '-1'
from if(keyIndex<keyword.length-1)
to if(keyIndex<keyword.length)
so that last keyword 'with' can be
found. Time stamp is 9909281142
3)
If input string use digits
+'freeman2.com/complex1.htm'
older version get error. Repair at
9909280550
<a name="doc108">Examples
4)
If input like
[[
aa
=
sin(PI/5);
]]
Old version get error. Now fixed.
When read to 'aa', peek to future
line, see if '=' is coming. If is
then not print aa value. The key
variable is manyLine1cmd. This
manyLine1cmd is wide spread.
<a name="doc109">
5)
If input like
[[
aa=6.5+
2-sin(PI/8)
]]
When read to '2-sin(PI/8)', program
look previous line, see if there is
'+' at line end. If yes, not print
'2-sin(PI/8)' value. Code location
is "if(i0>0) //9909291947"
<a name="doc110">
Plus few more minor changes.
This improved version is still not
complete compatible with javascript.
For example, example 15, there is
one line MUST insert /*=*/
<a name="doc111">
LiuHH can not take care all possible
cases. When you run your code, if
get error alert, check box 5 code.
See which line should not print
value but the string [outString+=]
is inserted in your code.
<a name="doc112">
To stop this insert, add five bytes
at the trouble line. it is "/*=*/".
Because complex2.htm do not care
/* comment */ then '=' in "/*=*/"
is visible to complex2.htm. When
see a '=', complex2.htm do not
insert [outString+=]
2010-09-29-21-03 stop
<a name="doc113">Examples
2010-09-29-21-11 start
update 2010-10-03 deleted document
from [a name="doc01"]
to [a name="doc77"]
Please goto complex2_20100726.htm
for old document.
Also see [a name="doc77"]<a name="doc114">
update 2010-10-03 added example 52
and 53.
Example 51 is complex number example
Example 52 is real number example
Example 53 is physics example
Example 51 is CSMC eqn.14.14
Example 52 is Euler Angle, quaternion
Example 53 is Kepler and Newton laws.
2010-09-29-21-16 stop
<a name="doc115">
2010-10-02-17-33 start
update 2010-10-03 allow Box3, JS command
use one line
//^ for insert one blank line at box4
//^//^ for insert two blank lines etc.
//^ insert nothing, must begin from
left end
LiuHH tried to insert code for blank
line at box4, this [//^] method will do
the job. Example 53 use the string [//^].
2010-10-02-17-41 stop
<a name="doc116">
2010-10-04-07-51 start
update 2010-10-04 made the following
two changes.
1) deleted "else" button. You can
find it at old page
Newer version (reading) use key
word table (look for 9908190809)
and avoid to print the value of
"else"
<a name="doc117">
2) added in-file stop point name.
2010-10-03-22-33 read
JavaScript-Bible-5e.pdf
chapter 17: Location and History
Objects.
2010-10-03-22-59 success use the
code line window.location.hash
Apply at [a name="doc77"] the
link of complex2_20100726.htm
2010-10-04-08-00 stop
<a name="doc118">
2010-10-18-10-32 start
update 2010-10-18 add an example
button "2^3=8" to show pow(a,b)
function.
update 2010-10-18 add
Convert degree to radian document
Please click <a name=JSMathFunc>
and look for "9910180956".
<a name="doc119">Examples
update 2010-10-18 add
//How to go around seesaw?
//It is very easy.
//Instead of the code
!true
(sin(PI/3)+cos(PI/3))/2
//write next code
var a1=!true
var a2=(sin(PI/3)+cos(PI/3))/2
a1
a2
//first two lines work in
//complex2_20100726.htm
//last four lines work in
//complex2.htm
2010-10-18-10-42 stop
<a name="doc120">
2010-10-24-13-30 start
update 2010-10-24 add example button 19
which solve x^i=i where i=sqrt(-1)
2010-10-24-13-32 stop
This file complex variable functions
Created on 2009-02-24
Chinese version first upload 2009-03-20
Chinese done version upload 2009-04-06
English version start translation on 2009-04-09-17-14
English version done translation on 2009-04-12-11-20