eigenmath/sin.cpp

377 lines
4.4 KiB
C++
Raw Permalink Normal View History

2005-10-16 00:10:22 +02:00
// Sine function of numerical and symbolic arguments
2004-06-18 01:02:29 +02:00
#include "stdafx.h"
2004-03-03 21:24:06 +01:00
#include "defs.h"
void
eval_sin(void)
{
push(cadr(p1));
eval();
sine();
}
void
sine(void)
{
save();
2008-05-09 21:45:29 +02:00
p1 = pop();
if (car(p1) == symbol(ADD))
sine_of_angle_sum();
else
sine_of_angle();
2004-03-03 21:24:06 +01:00
restore();
}
2008-05-09 21:45:29 +02:00
// Use angle sum formula for special angles.
#define A p3
#define B p4
2007-05-09 04:45:06 +02:00
void
2008-05-09 21:45:29 +02:00
sine_of_angle_sum(void)
2007-05-09 04:45:06 +02:00
{
p2 = cdr(p1);
while (iscons(p2)) {
2008-05-09 21:45:29 +02:00
B = car(p2);
if (isnpi(B)) {
push(p1);
push(B);
subtract();
A = pop();
push(A);
sine();
push(B);
cosine();
multiply();
push(A);
cosine();
push(B);
sine();
multiply();
add();
return;
}
2007-05-09 04:45:06 +02:00
p2 = cdr(p2);
}
2008-05-09 21:45:29 +02:00
sine_of_angle();
2007-05-09 04:45:06 +02:00
}
2005-10-15 22:41:33 +02:00
void
2008-05-09 21:45:29 +02:00
sine_of_angle(void)
2004-03-03 21:24:06 +01:00
{
int n;
2004-03-03 21:24:06 +01:00
double d;
2006-04-19 01:18:30 +02:00
if (car(p1) == symbol(ARCSIN)) {
push(cadr(p1));
return;
}
2005-08-06 22:57:37 +02:00
if (isdouble(p1)) {
2004-03-03 21:24:06 +01:00
d = sin(p1->u.d);
if (fabs(d) < 1e-10)
d = 0.0;
push_double(d);
return;
}
2007-05-09 04:45:06 +02:00
// sine function is antisymmetric, sin(-x) = -sin(x)
if (isnegative(p1)) {
push(p1);
negate();
sine();
negate();
return;
}
2006-02-11 21:11:26 +01:00
// sin(arctan(x)) = x / sqrt(1 + x^2)
// see p. 173 of the CRC Handbook of Mathematical Sciences
if (car(p1) == symbol(ARCTAN)) {
push(cadr(p1));
push_integer(1);
push(cadr(p1));
push_integer(2);
power();
add();
push_rational(-1, 2);
power();
multiply();
return;
}
2004-03-03 21:24:06 +01:00
// multiply by 180/pi
push(p1);
push_integer(180);
multiply();
push_symbol(PI);
divide();
n = pop_integer();
if (n < 0) {
2005-10-15 22:41:33 +02:00
push(symbol(SIN));
2004-03-03 21:24:06 +01:00
push(p1);
list(2);
return;
}
switch (n % 360) {
case 0:
2005-10-15 22:41:33 +02:00
case 180:
2004-03-03 21:24:06 +01:00
push_integer(0);
break;
case 30:
2005-10-15 22:41:33 +02:00
case 150:
push_rational(1, 2);
2004-03-03 21:24:06 +01:00
break;
2005-10-15 22:41:33 +02:00
case 210:
case 330:
push_rational(-1, 2);
break;
2005-10-08 04:20:34 +02:00
case 45:
2005-10-15 22:41:33 +02:00
case 135:
2005-10-08 04:20:34 +02:00
push_rational(1, 2);
push_integer(2);
push_rational(1, 2);
power();
multiply();
break;
2005-10-15 22:41:33 +02:00
case 225:
case 315:
push_rational(-1, 2);
push_integer(2);
2005-10-08 04:20:34 +02:00
push_rational(1, 2);
power();
multiply();
break;
2005-10-15 22:41:33 +02:00
case 60:
2005-10-08 04:20:34 +02:00
case 120:
push_rational(1, 2);
push_integer(3);
push_rational(1, 2);
power();
multiply();
break;
2005-10-15 22:41:33 +02:00
case 240:
case 300:
push_rational(-1, 2);
push_integer(3);
2005-10-08 04:20:34 +02:00
push_rational(1, 2);
power();
multiply();
break;
2005-10-15 22:41:33 +02:00
case 90:
push_integer(1);
2004-03-03 21:24:06 +01:00
break;
case 270:
push_integer(-1);
2004-03-03 21:24:06 +01:00
break;
default:
2005-10-15 22:41:33 +02:00
push(symbol(SIN));
2004-03-03 21:24:06 +01:00
push(p1);
list(2);
break;
}
}
2007-05-08 16:57:30 +02:00
#if SELFTEST
2004-03-03 21:24:06 +01:00
static char *s[] = {
2005-10-15 22:41:33 +02:00
"sin(x)",
"sin(x)",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"sin(-x)",
"-sin(x)",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"sin(b-a)",
"-sin(a-b)",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
// check against the floating point math library
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(a,x)=1+sin(float(a/360*2*pi))-float(x)+sin(a/360*2*pi)-x",
"",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(0,0)", // 0
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(90,1)", // 90
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(180,0)", // 180
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(270,-1)", // 270
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(360,0)", // 360
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-90,-1)", // -90
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-180,0)", // -180
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-270,1)", // -270
2004-03-03 21:24:06 +01:00
"1",
2005-10-15 22:41:33 +02:00
"f(-360,0)", // -360
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(45,sqrt(2)/2)", // 45
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(135,sqrt(2)/2)", // 135
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(225,-sqrt(2)/2)", // 225
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(315,-sqrt(2)/2)", // 315
"1",
2004-03-03 21:24:06 +01:00
2005-10-15 22:41:33 +02:00
"f(-45,-sqrt(2)/2)", // -45
"1",
2005-10-15 22:41:33 +02:00
"f(-135,-sqrt(2)/2)", // -135
"1",
2005-10-15 22:41:33 +02:00
"f(-225,sqrt(2)/2)", // -225
"1",
"f(-315,sqrt(2)/2)", // -315
"1",
"f(30,1/2)", // 30
"1",
"f(150,1/2)", // 150
"1",
"f(210,-1/2)", // 210
"1",
"f(330,-1/2)", // 330
"1",
"f(-30,-1/2)", // -30
"1",
"f(-150,-1/2)", // -150
"1",
"f(-210,1/2)", // -210
"1",
"f(-330,1/2)", // -330
"1",
"f(60,sqrt(3)/2)", // 60
"1",
"f(120,sqrt(3)/2)", // 120
"1",
"f(240,-sqrt(3)/2)", // 240
"1",
"f(300,-sqrt(3)/2)", // 300
"1",
"f(-60,-sqrt(3)/2)", // -60
"1",
"f(-120,-sqrt(3)/2)", // -120
"1",
"f(-240,sqrt(3)/2)", // -240
"1",
"f(-300,sqrt(3)/2)", // -300
"1",
"f=quote(f)",
"",
2006-04-19 01:18:30 +02:00
"sin(arcsin(x))",
"x",
2006-05-06 01:57:37 +02:00
2006-09-20 18:09:25 +02:00
// check the default case
"sin(1/12*pi)",
"sin(1/12*pi)",
2007-05-09 04:45:06 +02:00
"sin(arctan(4/3))",
"4/5",
"sin(-arctan(4/3))",
"-4/5",
// phase
"sin(x-8/2*pi)",
"sin(x)",
"sin(x-7/2*pi)",
"cos(x)",
"sin(x-6/2*pi)",
"-sin(x)",
"sin(x-5/2*pi)",
"-cos(x)",
"sin(x-4/2*pi)",
"sin(x)",
"sin(x-3/2*pi)",
"cos(x)",
"sin(x-2/2*pi)",
"-sin(x)",
"sin(x-1/2*pi)",
"-cos(x)",
"sin(x+0/2*pi)",
"sin(x)",
"sin(x+1/2*pi)",
"cos(x)",
"sin(x+2/2*pi)",
"-sin(x)",
"sin(x+3/2*pi)",
"-cos(x)",
"sin(x+4/2*pi)",
"sin(x)",
"sin(x+5/2*pi)",
"cos(x)",
"sin(x+6/2*pi)",
"-sin(x)",
"sin(x+7/2*pi)",
"-cos(x)",
"sin(x+8/2*pi)",
"sin(x)",
2004-03-03 21:24:06 +01:00
};
void
test_sin(void)
{
test(__FILE__, s, sizeof s / sizeof (char *));
}
2007-05-08 16:57:30 +02:00
#endif