123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- #include "stdafx.h"
- #include "defs.h"
- int
- iszero(U *p)
- {
- int i;
- switch (p->k) {
- case NUM:
- if (MZERO(p->u.q.a))
- return 1;
- break;
- case DOUBLE:
- if (p->u.d == 0.0)
- return 1;
- break;
- case TENSOR:
- for (i = 0; i < p->u.tensor->nelem; i++)
- if (!iszero(p->u.tensor->elem[i]))
- return 0;
- return 1;
- default:
- break;
- }
- return 0;
- }
- int
- isnegativenumber(U *p)
- {
- switch (p->k) {
- case NUM:
- if (MSIGN(p->u.q.a) == -1)
- return 1;
- break;
- case DOUBLE:
- if (p->u.d < 0.0)
- return 1;
- break;
- default:
- break;
- }
- return 0;
- }
- int
- isplusone(U *p)
- {
- switch (p->k) {
- case NUM:
- if (MEQUAL(p->u.q.a, 1) && MEQUAL(p->u.q.b, 1))
- return 1;
- break;
- case DOUBLE:
- if (p->u.d == 1.0)
- return 1;
- break;
- default:
- break;
- }
- return 0;
- }
- int
- isminusone(U *p)
- {
- switch (p->k) {
- case NUM:
- if (MEQUAL(p->u.q.a, -1) && MEQUAL(p->u.q.b, 1))
- return 1;
- break;
- case DOUBLE:
- if (p->u.d == -1.0)
- return 1;
- break;
- default:
- break;
- }
- return 0;
- }
- int
- isinteger(U *p)
- {
- if (p->k == NUM && MEQUAL(p->u.q.b, 1))
- return 1;
- else
- return 0;
- }
- int
- isnonnegativeinteger(U *p)
- {
- if (isrational(p) && MEQUAL(p->u.q.b, 1) && MSIGN(p->u.q.a) == 1)
- return 1;
- else
- return 0;
- }
- int
- isposint(U *p)
- {
- if (isinteger(p) && MSIGN(p->u.q.a) == 1)
- return 1;
- else
- return 0;
- }
- int
- ispoly(U *p, U *x)
- {
- if (find(p, x))
- return ispoly_expr(p, x);
- else
- return 0;
- }
- int
- ispoly_expr(U *p, U *x)
- {
- if (car(p) == symbol(ADD)) {
- p = cdr(p);
- while (iscons(p)) {
- if (!ispoly_term(car(p), x))
- return 0;
- p = cdr(p);
- }
- return 1;
- } else
- return ispoly_term(p, x);
- }
- int
- ispoly_term(U *p, U *x)
- {
- if (car(p) == symbol(MULTIPLY)) {
- p = cdr(p);
- while (iscons(p)) {
- if (!ispoly_factor(car(p), x))
- return 0;
- p = cdr(p);
- }
- return 1;
- } else
- return ispoly_factor(p, x);
- }
- int
- ispoly_factor(U *p, U *x)
- {
- if (equal(p, x))
- return 1;
- if (car(p) == symbol(POWER) && equal(cadr(p), x)) {
- if (isposint(caddr(p)))
- return 1;
- else
- return 0;
- }
- if (find(p, x))
- return 0;
- else
- return 1;
- }
- int
- isnegativeterm(U *p)
- {
- if (isnegativenumber(p))
- return 1;
- else if (car(p) == symbol(MULTIPLY) && isnegativenumber(cadr(p)))
- return 1;
- else
- return 0;
- }
- int
- isimaginarynumber(U *p)
- {
- if ((car(p) == symbol(MULTIPLY)
- && length(p) == 3
- && isnum(cadr(p))
- && equal(caddr(p), imaginaryunit))
- || equal(p, imaginaryunit))
- return 1;
- else
- return 0;
- }
- int
- iscomplexnumber(U *p)
- {
- if ((car(p) == symbol(ADD)
- && length(p) == 3
- && isnum(cadr(p))
- && isimaginarynumber(caddr(p)))
- || isimaginarynumber(p))
- return 1;
- else
- return 0;
- }
- int
- iseveninteger(U *p)
- {
- if (isinteger(p) && (p->u.q.a[0] & 1) == 0)
- return 1;
- else
- return 0;
- }
- int
- isnegative(U *p)
- {
- if (car(p) == symbol(ADD) && isnegativeterm(cadr(p)))
- return 1;
- else if (isnegativeterm(p))
- return 1;
- else
- return 0;
- }
- // returns 1 if there's a symbol somewhere
- int
- issymbolic(U *p)
- {
- if (issymbol(p))
- return 1;
- else {
- while (iscons(p)) {
- if (issymbolic(car(p)))
- return 1;
- p = cdr(p);
- }
- return 0;
- }
- }
- // i.e. 2, 2^3, etc.
- int
- isintegerfactor(U *p)
- {
- if (isinteger(p) || car(p) == symbol(POWER)
- && isinteger(cadr(p))
- && isinteger(caddr(p)))
- return 1;
- else
- return 0;
- }
- int
- isoneover(U *p)
- {
- if (car(p) == symbol(POWER)
- && isminusone(caddr(p)))
- return 1;
- else
- return 0;
- }
- int
- isfraction(U *p)
- {
- if (p->k == NUM && !MEQUAL(p->u.q.b, 1))
- return 1;
- else
- return 0;
- }
- int
- equaln(U *p, int n)
- {
- switch (p->k) {
- case NUM:
- if (MEQUAL(p->u.q.a, n) && MEQUAL(p->u.q.b, 1))
- return 1;
- break;
- case DOUBLE:
- if (p->u.d == (double) n)
- return 1;
- break;
- default:
- break;
- }
- return 0;
- }
- int
- equalq(U *p, int a, int b)
- {
- switch (p->k) {
- case NUM:
- if (MEQUAL(p->u.q.a, a) && MEQUAL(p->u.q.b, b))
- return 1;
- break;
- case DOUBLE:
- if (p->u.d == (double) a / b)
- return 1;
- break;
- default:
- break;
- }
- return 0;
- }
- // p == 1/sqrt(2) ?
- int
- isoneoversqrttwo(U *p)
- {
- if (car(p) == symbol(POWER)
- && equaln(cadr(p), 2)
- && equalq(caddr(p), -1, 2))
- return 1;
- else
- return 0;
- }
- // p == -1/sqrt(2) ?
- int
- isminusoneoversqrttwo(U *p)
- {
- if (car(p) == symbol(MULTIPLY)
- && equaln(cadr(p), -1)
- && isoneoversqrttwo(caddr(p))
- && length(p) == 3)
- return 1;
- else
- return 0;
- }
- int
- isfloating(U *p)
- {
- if (p->k == DOUBLE)
- return 1;
- while (iscons(p)) {
- if (isfloating(car(p)))
- return 1;
- p = cdr(p);
- }
- return 0;
- }
- int
- isimaginaryunit(U *p)
- {
- if (equal(p, imaginaryunit))
- return 1;
- else
- return 0;
- }
- // n/2 * i * pi ?
- // return value:
- // 0 no
- // 1 1
- // 2 -1
- // 3 i
- // 4 -i
- int
- isquarterturn(U *p)
- {
- int n, minussign = 0;
- if (car(p) != symbol(MULTIPLY))
- return 0;
- if (equal(cadr(p), imaginaryunit)) {
- if (caddr(p) != symbol(PI))
- return 0;
- if (length(p) != 3)
- return 0;
- return 2;
- }
- if (!isnum(cadr(p)))
- return 0;
- if (!equal(caddr(p), imaginaryunit))
- return 0;
- if (cadddr(p) != symbol(PI))
- return 0;
- if (length(p) != 4)
- return 0;
- push(cadr(p));
- push_integer(2);
- multiply();
- n = pop_integer();
- if (n == (int) 0x80000000)
- return 0;
- if (n < 1) {
- minussign = 1;
- n = -n;
- }
- switch (n % 4) {
- case 0:
- n = 1;
- break;
- case 1:
- if (minussign)
- n = 4;
- else
- n = 3;
- break;
- case 2:
- n = 2;
- break;
- case 3:
- if (minussign)
- n = 3;
- else
- n = 4;
- break;
- }
- return n;
- }
- // special multiple of pi?
- // returns for the following multiples of pi...
- // -4/2 -3/2 -2/2 -1/2 1/2 2/2 3/2 4/2
- // 4 1 2 3 1 2 3 4
- int
- isnpi(U *p)
- {
- int n;
- if (p == symbol(PI))
- return 2;
- if (car(p) == symbol(MULTIPLY)
- && isnum(cadr(p))
- && caddr(p) == symbol(PI)
- && length(p) == 3)
- ;
- else
- return 0;
- push(cadr(p));
- push_integer(2);
- multiply();
- n = pop_integer();
- if (n == (int) 0x80000000)
- return 0;
- if (n < 0)
- n = 4 - (-n) % 4;
- else
- n = 1 + (n - 1) % 4;
- return n;
- }
|