1 Star 0 Fork 0

zhouxs1023/eigenmath_pratt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
abs.cpp 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
Calin Barbat 提交于 2018-02-24 10:52 . Initial commit.
// Absolute value, aka vector magnitude
#include "stdafx.h"
#include "defs.h"
void
eval_abs(void)
{
push(cadr(p1));
eval();
absval();
}
void
absval(void)
{
int h;
save();
p1 = pop();
if (istensor(p1)) {
absval_tensor();
restore();
return;
}
if (isnum(p1)) {
push(p1);
if (isnegativenumber(p1))
negate();
restore();
return;
}
if (iscomplexnumber(p1)) {
push(p1);
push(p1);
conjugate();
multiply();
push_rational(1, 2);
power();
restore();
return;
}
// abs(1/a) evaluates to 1/abs(a)
if (car(p1) == symbol(POWER) && isnegativeterm(caddr(p1))) {
push(p1);
reciprocate();
absval();
reciprocate();
restore();
return;
}
// abs(a*b) evaluates to abs(a)*abs(b)
if (car(p1) == symbol(MULTIPLY)) {
h = tos;
p1 = cdr(p1);
while (iscons(p1)) {
push(car(p1));
absval();
p1 = cdr(p1);
}
multiply_all(tos - h);
restore();
return;
}
if (isnegativeterm(p1) || (car(p1) == symbol(ADD) && isnegativeterm(cadr(p1)))) {
push(p1);
negate();
p1 = pop();
}
push_symbol(ABS);
push(p1);
list(2);
restore();
}
void
absval_tensor(void)
{
if (p1->u.tensor->ndim != 1)
stop("abs(tensor) with tensor rank > 1");
push(p1);
push(p1);
conjugate();
inner();
push_rational(1, 2);
power();
simplify();
eval();
}
#if SELFTEST
static const char *s[] = {
"abs(2)",
"2",
"abs(2.0)",
"2",
"abs(-2)",
"2",
"abs(-2.0)",
"2",
"abs(a)",
"abs(a)",
"abs(-a)",
"abs(a)",
"abs(2*a)",
"2*abs(a)",
"abs(-2*a)",
"2*abs(a)",
"abs(2.0*a)",
"2*abs(a)",
"abs(-2.0*a)",
"2*abs(a)",
"abs(a-b)+abs(b-a)",
"2*abs(a-b)",
"abs(3 + 4 i)",
"5",
"abs((2,3,4))",
"29^(1/2)",
"abs(a*b)",
"abs(a)*abs(b)",
"abs(a/b)",
"abs(a)/abs(b)",
"abs(1/a^b)",
"1/(abs(a^b))",
// Check that vector length is simplified
"P=(u*cos(v),u*sin(v),v)",
"",
"abs(cross(d(P,u),d(P,v)))",
"(1+u^2)^(1/2)",
};
void
test_abs(void)
{
test(__FILE__, s, sizeof s / sizeof (char *));
}
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/zhouxs1023/eigenmath_pratt.git
git@gitee.com:zhouxs1023/eigenmath_pratt.git
zhouxs1023
eigenmath_pratt
eigenmath_pratt
master

搜索帮助