eigenmath/product.cpp

95 lines
1.2 KiB
C++
Raw Permalink Normal View History

2006-10-06 20:28:26 +02:00
/* Name: product
Syntax: product(var, minval, maxval, expr)
Input: cadr(p1) var
caddr(p1) minval
cadddr(p1) maxval
caddddr(p1) expr
Output: Product on stack
Notes: The original value of var is restored before the
product function returns.
Expr is quoted. It is not evaluated until var is
assigned minval.
*/
2004-03-03 21:24:06 +01:00
#include "stdafx.h"
#include "defs.h"
#define F p1
#define X p2
void
eval_product(void)
{
2006-10-13 17:26:03 +02:00
push(cadr(p1)); /* index expr is quoted */
2004-03-03 21:24:06 +01:00
push(caddr(p1));
eval();
push(cadddr(p1));
eval();
push(caddddr(p1));
product();
}
2006-10-06 20:28:26 +02:00
void
2004-03-03 21:24:06 +01:00
product(void)
{
2006-10-06 20:28:26 +02:00
int h, i, j, k;
2004-03-03 21:24:06 +01:00
F = pop();
k = pop_integer();
j = pop_integer();
X = pop();
if (j == (int) 0x80000000)
stop("2nd arg in product function: integer expected");
if (k == (int) 0x80000000)
stop("3rd arg in product function: integer expected");
2006-10-06 20:28:26 +02:00
h = tos;
2004-03-03 21:24:06 +01:00
for (i = j; i <= k; i++) {
push(F);
push(X);
push_integer(i);
2006-10-06 20:28:26 +02:00
subst();
eval();
2004-03-03 21:24:06 +01:00
}
2006-10-06 20:28:26 +02:00
multiply_all(tos - h);
2004-03-03 21:24:06 +01:00
}
2007-05-08 16:57:30 +02:00
#if SELFTEST
2004-03-03 21:24:06 +01:00
static char *s[] = {
"n=quote(n)",
"",
"product(n,1,4,n)",
"24",
"n",
"n",
2006-10-13 17:26:03 +02:00
/* ensure index expr is quoted */
"product(i,1,3,i)",
"6",
2004-03-03 21:24:06 +01:00
};
void
test_product(void)
{
test(__FILE__, s, sizeof s / sizeof (char *));
}
2007-05-08 16:57:30 +02:00
#endif