1 Star 0 Fork 0

zhouxs1023/eigenmath_pratt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
tensor.cpp 9.37 KB
一键复制 编辑 原始数据 按行查看 历史
Calin Barbat 提交于 2018-02-24 10:52 . Initial commit.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
#include "stdafx.h"
#include "defs.h"
static void promote_tensor(void);
static int compatible(U *, U *);
//-----------------------------------------------------------------------------
//
// Called from the "eval" module to evaluate tensor elements.
//
// p1 points to the tensor operand.
//
//-----------------------------------------------------------------------------
void
eval_tensor(void)
{
int i, ndim, nelem;
U **a, **b;
//---------------------------------------------------------------------
//
// create a new tensor for the result
//
//---------------------------------------------------------------------
nelem = p1->u.tensor->nelem;
ndim = p1->u.tensor->ndim;
p2 = alloc_tensor(nelem);
p2->u.tensor->ndim = ndim;
for (i = 0; i < ndim; i++)
p2->u.tensor->dim[i] = p1->u.tensor->dim[i];
//---------------------------------------------------------------------
//
// b = eval(a)
//
//---------------------------------------------------------------------
a = p1->u.tensor->elem;
b = p2->u.tensor->elem;
for (i = 0; i < nelem; i++) {
push(a[i]);
eval();
b[i] = pop();
}
//---------------------------------------------------------------------
//
// push the result
//
//---------------------------------------------------------------------
push(p2);
promote_tensor();
}
//-----------------------------------------------------------------------------
//
// Add tensors
//
// Input: Operands on stack
//
// Output: Result on stack
//
//-----------------------------------------------------------------------------
void
tensor_plus_tensor(void)
{
int i, ndim, nelem;
U **a, **b, **c;
save();
p2 = pop();
p1 = pop();
// are the dimension lists equal?
ndim = p1->u.tensor->ndim;
if (ndim != p2->u.tensor->ndim) {
push(symbol(NIL));
restore();
return;
}
for (i = 0; i < ndim; i++)
if (p1->u.tensor->dim[i] != p2->u.tensor->dim[i]) {
push(symbol(NIL));
restore();
return;
}
// create a new tensor for the result
nelem = p1->u.tensor->nelem;
p3 = alloc_tensor(nelem);
p3->u.tensor->ndim = ndim;
for (i = 0; i < ndim; i++)
p3->u.tensor->dim[i] = p1->u.tensor->dim[i];
// c = a + b
a = p1->u.tensor->elem;
b = p2->u.tensor->elem;
c = p3->u.tensor->elem;
for (i = 0; i < nelem; i++) {
push(a[i]);
push(b[i]);
add();
c[i] = pop();
}
// push the result
push(p3);
restore();
}
//-----------------------------------------------------------------------------
//
// careful not to reorder factors
//
//-----------------------------------------------------------------------------
void
tensor_times_scalar(void)
{
int i, ndim, nelem;
U **a, **b;
save();
p2 = pop();
p1 = pop();
ndim = p1->u.tensor->ndim;
nelem = p1->u.tensor->nelem;
p3 = alloc_tensor(nelem);
p3->u.tensor->ndim = ndim;
for (i = 0; i < ndim; i++)
p3->u.tensor->dim[i] = p1->u.tensor->dim[i];
a = p1->u.tensor->elem;
b = p3->u.tensor->elem;
for (i = 0; i < nelem; i++) {
push(a[i]);
push(p2);
multiply();
b[i] = pop();
}
push(p3);
restore();
}
void
scalar_times_tensor(void)
{
int i, ndim, nelem;
U **a, **b;
save();
p2 = pop();
p1 = pop();
ndim = p2->u.tensor->ndim;
nelem = p2->u.tensor->nelem;
p3 = alloc_tensor(nelem);
p3->u.tensor->ndim = ndim;
for (i = 0; i < ndim; i++)
p3->u.tensor->dim[i] = p2->u.tensor->dim[i];
a = p2->u.tensor->elem;
b = p3->u.tensor->elem;
for (i = 0; i < nelem; i++) {
push(p1);
push(a[i]);
multiply();
b[i] = pop();
}
push(p3);
restore();
}
int
is_square_matrix(U *p)
{
if (istensor(p) && p->u.tensor->ndim == 2 && p->u.tensor->dim[0] == p->u.tensor->dim[1])
return 1;
else
return 0;
}
//-----------------------------------------------------------------------------
//
// gradient of tensor
//
//-----------------------------------------------------------------------------
void
d_tensor_tensor(void)
{
int i, j, ndim, nelem;
U **a, **b, **c;
ndim = p1->u.tensor->ndim;
nelem = p1->u.tensor->nelem;
if (ndim + 1 >= MAXDIM)
goto dont_evaluate;
p3 = alloc_tensor(nelem * p2->u.tensor->nelem);
p3->u.tensor->ndim = ndim + 1;
for (i = 0; i < ndim; i++)
p3->u.tensor->dim[i] = p1->u.tensor->dim[i];
p3->u.tensor->dim[ndim] = p2->u.tensor->dim[0];
a = p1->u.tensor->elem;
b = p2->u.tensor->elem;
c = p3->u.tensor->elem;
for (i = 0; i < nelem; i++) {
for (j = 0; j < p2->u.tensor->nelem; j++) {
push(a[i]);
push(b[j]);
derivative();
c[i * p2->u.tensor->nelem + j] = pop();
}
}
push(p3);
return;
dont_evaluate:
push_symbol(DERIVATIVE);
push(p1);
push(p2);
list(3);
}
//-----------------------------------------------------------------------------
//
// gradient of scalar
//
//-----------------------------------------------------------------------------
void
d_scalar_tensor(void)
{
int i;
U **a, **b;
p3 = alloc_tensor(p2->u.tensor->nelem);
p3->u.tensor->ndim = 1;
p3->u.tensor->dim[0] = p2->u.tensor->dim[0];
a = p2->u.tensor->elem;
b = p3->u.tensor->elem;
for (i = 0; i < p2->u.tensor->nelem; i++) {
push(p1);
push(a[i]);
derivative();
b[i] = pop();
}
push(p3);
}
//-----------------------------------------------------------------------------
//
// Derivative of tensor
//
//-----------------------------------------------------------------------------
void
d_tensor_scalar(void)
{
int i;
U **a, **b;
p3 = alloc_tensor(p1->u.tensor->nelem);
p3->u.tensor->ndim = p1->u.tensor->ndim;
for (i = 0; i < p1->u.tensor->ndim; i++)
p3->u.tensor->dim[i] = p1->u.tensor->dim[i];
a = p1->u.tensor->elem;
b = p3->u.tensor->elem;
for (i = 0; i < p1->u.tensor->nelem; i++) {
push(a[i]);
push(p2);
derivative();
b[i] = pop();
}
push(p3);
}
int
compare_tensors(U *p1, U *p2)
{
int i;
if (p1->u.tensor->ndim < p2->u.tensor->ndim)
return -1;
if (p1->u.tensor->ndim > p2->u.tensor->ndim)
return 1;
for (i = 0; i < p1->u.tensor->ndim; i++) {
if (p1->u.tensor->dim[i] < p2->u.tensor->dim[i])
return -1;
if (p1->u.tensor->dim[i] > p2->u.tensor->dim[i])
return 1;
}
for (i = 0; i < p1->u.tensor->nelem; i++) {
if (equal(p1->u.tensor->elem[i], p2->u.tensor->elem[i]))
continue;
if (lessp(p1->u.tensor->elem[i], p2->u.tensor->elem[i]))
return -1;
else
return 1;
}
return 0;
}
//-----------------------------------------------------------------------------
//
// Raise a tensor to a power
//
// Input: p1 tensor
//
// p2 exponent
//
// Output: Result on stack
//
//-----------------------------------------------------------------------------
void
power_tensor(void)
{
int i, k, n;
// first and last dims must be equal
k = p1->u.tensor->ndim - 1;
if (p1->u.tensor->dim[0] != p1->u.tensor->dim[k]) {
push_symbol(POWER);
push(p1);
push(p2);
list(3);
return;
}
push(p2);
n = pop_integer();
if (n == (int) 0x80000000) {
push_symbol(POWER);
push(p1);
push(p2);
list(3);
return;
}
if (n == 0) {
if (p1->u.tensor->ndim != 2)
stop("power(tensor,0) with tensor rank not equal to 2");
n = p1->u.tensor->dim[0];
p1 = alloc_tensor(n * n);
p1->u.tensor->ndim = 2;
p1->u.tensor->dim[0] = n;
p1->u.tensor->dim[1] = n;
for (i = 0; i < n; i++)
p1->u.tensor->elem[n * i + i] = one;
push(p1);
return;
}
if (n < 0) {
n = -n;
push(p1);
inv();
p1 = pop();
}
push(p1);
for (i = 1; i < n; i++) {
push(p1);
inner();
if (iszero(stack[tos - 1]))
break;
}
}
void
copy_tensor(void)
{
int i;
save();
p1 = pop();
p2 = alloc_tensor(p1->u.tensor->nelem);
p2->u.tensor->ndim = p1->u.tensor->ndim;
for (i = 0; i < p1->u.tensor->ndim; i++)
p2->u.tensor->dim[i] = p1->u.tensor->dim[i];
for (i = 0; i < p1->u.tensor->nelem; i++)
p2->u.tensor->elem[i] = p1->u.tensor->elem[i];
push(p2);
restore();
}
// Tensors with elements that are also tensors get promoted to a higher rank.
static void
promote_tensor(void)
{
int i, j, k, nelem, ndim;
save();
p1 = pop();
if (!istensor(p1)) {
push(p1);
restore();
return;
}
p2 = p1->u.tensor->elem[0];
for (i = 1; i < p1->u.tensor->nelem; i++)
if (!compatible(p2, p1->u.tensor->elem[i]))
stop("Cannot promote tensor due to inconsistent tensor components.");
if (!istensor(p2)) {
push(p1);
restore();
return;
}
ndim = p1->u.tensor->ndim + p2->u.tensor->ndim;
if (ndim > MAXDIM)
stop("tensor rank > 24");
nelem = p1->u.tensor->nelem * p2->u.tensor->nelem;
p3 = alloc_tensor(nelem);
p3->u.tensor->ndim = ndim;
for (i = 0; i < p1->u.tensor->ndim; i++)
p3->u.tensor->dim[i] = p1->u.tensor->dim[i];
for (j = 0; j < p2->u.tensor->ndim; j++)
p3->u.tensor->dim[i + j] = p2->u.tensor->dim[j];
k = 0;
for (i = 0; i < p1->u.tensor->nelem; i++) {
p2 = p1->u.tensor->elem[i];
for (j = 0; j < p2->u.tensor->nelem; j++)
p3->u.tensor->elem[k++] = p2->u.tensor->elem[j];
}
push(p3);
restore();
}
static int
compatible(U *p, U *q)
{
int i;
if (!istensor(p) && !istensor(q))
return 1;
if (!istensor(p) || !istensor(q))
return 0;
if (p->u.tensor->ndim != q->u.tensor->ndim)
return 0;
for (i = 0; i < p->u.tensor->ndim; i++)
if (p->u.tensor->dim[i] != q->u.tensor->dim[i])
return 0;
return 1;
}
#if SELFTEST
static const char *s[] = {
"#test_tensor",
"a=(1,2,3)",
"",
"b=(4,5,6)",
"",
"c=(7,8,9)",
"",
"rank((a,b,c))",
"2",
"(a,b,c)",
"((1,2,3),(4,5,6),(7,8,9))",
// check tensor promotion
"((1,0),(0,0))",
"((1,0),(0,0))",
"a=quote(a)",
"",
"b=quote(b)",
"",
"c=quote(c)",
"",
};
void
test_tensor(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

搜索帮助