Another file is close to the NuttX coding standard
This commit is contained in:
parent
02b974859c
commit
693293cc59
@ -1,4 +1,7 @@
|
|||||||
/* #includes */ /*{{{C}}}*//*{{{*/
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
#undef _POSIX_SOURCE
|
#undef _POSIX_SOURCE
|
||||||
#define _POSIX_SOURCE 1
|
#define _POSIX_SOURCE 1
|
||||||
#undef _POSIX_C_SOURCE
|
#undef _POSIX_C_SOURCE
|
||||||
@ -22,9 +25,12 @@
|
|||||||
#ifdef USE_DMALLOC
|
#ifdef USE_DMALLOC
|
||||||
# include "dmalloc.h"
|
# include "dmalloc.h"
|
||||||
#endif
|
#endif
|
||||||
/*}}}*/
|
|
||||||
|
|
||||||
struct Var *Var_new(struct Var *this, enum ValueType type, unsigned int dim, const unsigned int *geometry, int base) /*{{{*/
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
struct Var *Var_new(struct Var *this, enum ValueType type, unsigned int dim, const unsigned int *geometry, int base)
|
||||||
{
|
{
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
size_t newsize;
|
size_t newsize;
|
||||||
@ -34,25 +40,32 @@ struct Var *Var_new(struct Var *this, enum ValueType type, unsigned int dim, con
|
|||||||
this->base = base;
|
this->base = base;
|
||||||
for (newsize = this->size = 1, dim = 0; dim < this->dim; ++dim)
|
for (newsize = this->size = 1, dim = 0; dim < this->dim; ++dim)
|
||||||
{
|
{
|
||||||
if ((newsize*=geometry[dim])<this->size) return (struct Var*)0;
|
if ((newsize *= geometry[dim]) < this->size)
|
||||||
|
return (struct Var *)0;
|
||||||
this->size = newsize;
|
this->size = newsize;
|
||||||
}
|
}
|
||||||
if ((newsize*=sizeof(struct Value))<this->size) return (struct Var*)0;
|
if ((newsize *= sizeof(struct Value)) < this->size)
|
||||||
if ((this->value=malloc(newsize))==(struct Value*)0) return (struct Var*)0;
|
return (struct Var *)0;
|
||||||
|
if ((this->value = malloc(newsize)) == (struct Value *)0)
|
||||||
|
return (struct Var *)0;
|
||||||
if (dim)
|
if (dim)
|
||||||
{
|
{
|
||||||
this->geometry = malloc(sizeof(unsigned int) * dim);
|
this->geometry = malloc(sizeof(unsigned int) * dim);
|
||||||
for (i=0; i<dim; ++i) this->geometry[i]=geometry[i];
|
for (i = 0; i < dim; ++i)
|
||||||
|
this->geometry[i] = geometry[i];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this->geometry = (unsigned int *)0;
|
this->geometry = (unsigned int *)0;
|
||||||
}
|
}
|
||||||
for (i=0; i<this->size; ++i) Value_new_null(&(this->value[i]),type);
|
|
||||||
|
for (i = 0; i < this->size; ++i)
|
||||||
|
Value_new_null(&(this->value[i]), type);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
|
||||||
struct Var *Var_new_scalar(struct Var *this) /*{{{*/
|
struct Var *Var_new_scalar(struct Var *this)
|
||||||
{
|
{
|
||||||
this->dim = 0;
|
this->dim = 0;
|
||||||
this->size = 1;
|
this->size = 1;
|
||||||
@ -60,10 +73,12 @@ struct Var *Var_new_scalar(struct Var *this) /*{{{*/
|
|||||||
this->value = malloc(sizeof(struct Value));
|
this->value = malloc(sizeof(struct Value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
|
||||||
void Var_destroy(struct Var *this) /*{{{*/
|
void Var_destroy(struct Var *this)
|
||||||
{
|
{
|
||||||
while (this->size--) Value_destroy(&(this->value[this->size]));
|
while (this->size--)
|
||||||
|
Value_destroy(&(this->value[this->size]));
|
||||||
|
|
||||||
free(this->value);
|
free(this->value);
|
||||||
this->value = (struct Value *)0;
|
this->value = (struct Value *)0;
|
||||||
this->size = 0;
|
this->size = 0;
|
||||||
@ -74,8 +89,8 @@ void Var_destroy(struct Var *this) /*{{{*/
|
|||||||
this->geometry = (unsigned int *)0;
|
this->geometry = (unsigned int *)0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*}}}*/
|
|
||||||
void Var_retype(struct Var *this, enum ValueType type) /*{{{*/
|
void Var_retype(struct Var *this, enum ValueType type)
|
||||||
{
|
{
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
@ -85,14 +100,16 @@ void Var_retype(struct Var *this, enum ValueType type) /*{{{*/
|
|||||||
Value_new_null(&(this->value[i]), type);
|
Value_new_null(&(this->value[i]), type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*}}}*/
|
|
||||||
struct Value *Var_value(struct Var *this, unsigned int dim, int idx[], struct Value *value) /*{{{*/
|
struct Value *Var_value(struct Var *this, unsigned int dim, int idx[], struct Value *value)
|
||||||
{
|
{
|
||||||
unsigned int offset;
|
unsigned int offset;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
assert(this->value);
|
assert(this->value);
|
||||||
if (dim!=this->dim) return Value_new_ERROR(value,DIMENSION);
|
if (dim != this->dim)
|
||||||
|
return Value_new_ERROR(value, DIMENSION);
|
||||||
|
|
||||||
for (offset = 0, i = 0; i < dim; ++i)
|
for (offset = 0, i = 0; i < dim; ++i)
|
||||||
{
|
{
|
||||||
if (idx[i] < this->base || (idx[i] - this->base) >= this->geometry[i])
|
if (idx[i] < this->base || (idx[i] - this->base) >= this->geometry[i])
|
||||||
@ -101,11 +118,12 @@ struct Value *Var_value(struct Var *this, unsigned int dim, int idx[], struct Va
|
|||||||
}
|
}
|
||||||
offset = offset * this->geometry[i] + (idx[i] - this->base);
|
offset = offset * this->geometry[i] + (idx[i] - this->base);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(offset < this->size);
|
assert(offset < this->size);
|
||||||
return this->value + offset;
|
return this->value + offset;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
|
||||||
void Var_clear(struct Var *this) /*{{{*/
|
void Var_clear(struct Var *this)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
@ -113,6 +131,7 @@ void Var_clear(struct Var *this) /*{{{*/
|
|||||||
{
|
{
|
||||||
Value_destroy(&(this->value[i]));
|
Value_destroy(&(this->value[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->geometry)
|
if (this->geometry)
|
||||||
{
|
{
|
||||||
free(this->geometry);
|
free(this->geometry);
|
||||||
@ -120,10 +139,11 @@ void Var_clear(struct Var *this) /*{{{*/
|
|||||||
this->size = 1;
|
this->size = 1;
|
||||||
this->dim = 0;
|
this->dim = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value_new_null(&(this->value[0]), this->type);
|
Value_new_null(&(this->value[0]), this->type);
|
||||||
}
|
}
|
||||||
/*}}}*/
|
|
||||||
struct Value *Var_mat_assign(struct Var *this, struct Var *x, struct Value *err, int work) /*{{{*/
|
struct Value *Var_mat_assign(struct Var *this, struct Var *x, struct Value *err, int work)
|
||||||
{
|
{
|
||||||
enum ValueType thisType = this->type;
|
enum ValueType thisType = this->type;
|
||||||
|
|
||||||
@ -135,12 +155,14 @@ struct Value *Var_mat_assign(struct Var *this, struct Var *x, struct Value *err,
|
|||||||
|
|
||||||
assert(x->base == 0 || x->base == 1);
|
assert(x->base == 0 || x->base == 1);
|
||||||
assert(x->dim == 1 || x->dim == 2);
|
assert(x->dim == 1 || x->dim == 2);
|
||||||
if (this==x) return (struct Value*)0;
|
if (this == x)
|
||||||
|
return (struct Value *)0;
|
||||||
Var_destroy(this);
|
Var_destroy(this);
|
||||||
Var_new(this, thisType, x->dim, x->geometry, x->base);
|
Var_new(this, thisType, x->dim, x->geometry, x->base);
|
||||||
g0 = x->geometry[0];
|
g0 = x->geometry[0];
|
||||||
g1 = x->dim == 1 ? unused + 1 : x->geometry[1];
|
g1 = x->dim == 1 ? unused + 1 : x->geometry[1];
|
||||||
for (i=unused; i<g0; ++i) for (j=unused; j<g1; ++j)
|
for (i = unused; i < g0; ++i)
|
||||||
|
for (j = unused; j < g1; ++j)
|
||||||
{
|
{
|
||||||
unsigned int element = x->dim == 1 ? i : i * g1 + j;
|
unsigned int element = x->dim == 1 ? i : i * g1 + j;
|
||||||
|
|
||||||
@ -151,12 +173,14 @@ struct Value *Var_mat_assign(struct Var *this, struct Var *x, struct Value *err,
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (Value_commonType[this->type][x->type]==V_ERROR) return Value_new_typeError(err,this->type,x->type);
|
if (Value_commonType[this->type][x->type] == V_ERROR)
|
||||||
|
return Value_new_typeError(err, this->type, x->type);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (struct Value *)0;
|
return (struct Value *)0;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
|
||||||
struct Value *Var_mat_addsub(struct Var *this, struct Var *x, struct Var *y, int add, struct Value *err, int work) /*{{{*/
|
struct Value *Var_mat_addsub(struct Var *this, struct Var *x, struct Var *y, int add, struct Value *err, int work)
|
||||||
{
|
{
|
||||||
enum ValueType thisType = this->type;
|
enum ValueType thisType = this->type;
|
||||||
struct Value foo, bar;
|
struct Value foo, bar;
|
||||||
@ -169,22 +193,31 @@ struct Value *Var_mat_addsub(struct Var *this, struct Var *x, struct Var *y, int
|
|||||||
|
|
||||||
assert(x->base == 0 || x->base == 1);
|
assert(x->base == 0 || x->base == 1);
|
||||||
assert(x->dim == 1 || x->dim == 2);
|
assert(x->dim == 1 || x->dim == 2);
|
||||||
if (x->base!=y->base || x->dim!=y->dim || x->geometry[0]!=y->geometry[0] || (x->dim==2 && x->geometry[1]!=y->geometry[1])) return Value_new_ERROR(err,DIMENSION);
|
if (x->base != y->base || x->dim != y->dim ||
|
||||||
|
x->geometry[0] != y->geometry[0] || (x->dim == 2 &&
|
||||||
|
x->geometry[1] !=
|
||||||
|
y->geometry[1]))
|
||||||
|
return Value_new_ERROR(err, DIMENSION);
|
||||||
|
|
||||||
if (this != x && this != y)
|
if (this != x && this != y)
|
||||||
{
|
{
|
||||||
Var_destroy(this);
|
Var_destroy(this);
|
||||||
Var_new(this, thisType, x->dim, x->geometry, x->base);
|
Var_new(this, thisType, x->dim, x->geometry, x->base);
|
||||||
}
|
}
|
||||||
|
|
||||||
g0 = x->geometry[0];
|
g0 = x->geometry[0];
|
||||||
g1 = x->dim == 1 ? unused + 1 : x->geometry[1];
|
g1 = x->dim == 1 ? unused + 1 : x->geometry[1];
|
||||||
for (i=unused; i<g0; ++i) for (j=unused; j<g1; ++j)
|
for (i = unused; i < g0; ++i)
|
||||||
|
for (j = unused; j < g1; ++j)
|
||||||
{
|
{
|
||||||
unsigned int element = x->dim == 1 ? i : i * g1 + j;
|
unsigned int element = x->dim == 1 ? i : i * g1 + j;
|
||||||
|
|
||||||
Value_clone(&foo, &(x->value[element]));
|
Value_clone(&foo, &(x->value[element]));
|
||||||
Value_clone(&bar, &(y->value[element]));
|
Value_clone(&bar, &(y->value[element]));
|
||||||
if (add) Value_add(&foo,&bar,1);
|
if (add)
|
||||||
else Value_sub(&foo,&bar,1);
|
Value_add(&foo, &bar, 1);
|
||||||
|
else
|
||||||
|
Value_sub(&foo, &bar, 1);
|
||||||
if (foo.type == V_ERROR)
|
if (foo.type == V_ERROR)
|
||||||
{
|
{
|
||||||
*err = foo;
|
*err = foo;
|
||||||
@ -199,15 +232,21 @@ struct Value *Var_mat_addsub(struct Var *this, struct Var *x, struct Var *y, int
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
Value_clone(err, x->value);
|
Value_clone(err, x->value);
|
||||||
if (add) Value_add(err,y->value,0);
|
if (add)
|
||||||
else Value_sub(err,y->value,0);
|
Value_add(err, y->value, 0);
|
||||||
if (err->type==V_ERROR) return err;
|
else
|
||||||
|
Value_sub(err, y->value, 0);
|
||||||
|
|
||||||
|
if (err->type == V_ERROR)
|
||||||
|
return err;
|
||||||
|
|
||||||
Value_destroy(err);
|
Value_destroy(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (struct Value *)0;
|
return (struct Value *)0;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
|
||||||
struct Value *Var_mat_mult(struct Var *this, struct Var *x, struct Var *y, struct Value *err, int work) /*{{{*/
|
struct Value *Var_mat_mult(struct Var *this, struct Var *x, struct Var *y, struct Value *err, int work)
|
||||||
{
|
{
|
||||||
enum ValueType thisType = this->type;
|
enum ValueType thisType = this->type;
|
||||||
struct Var foo;
|
struct Var foo;
|
||||||
@ -219,11 +258,15 @@ struct Value *Var_mat_mult(struct Var *this, struct Var *x, struct Var *y, struc
|
|||||||
int unused = 1 - x->base;
|
int unused = 1 - x->base;
|
||||||
|
|
||||||
assert(x->base == 0 || x->base == 1);
|
assert(x->base == 0 || x->base == 1);
|
||||||
if (x->dim!=2 || y->dim!=2 || x->base!=y->base || x->geometry[1]!=y->geometry[0]) return Value_new_ERROR(err,DIMENSION);
|
if (x->dim != 2 || y->dim != 2 || x->base != y->base ||
|
||||||
|
x->geometry[1] != y->geometry[0])
|
||||||
|
return Value_new_ERROR(err, DIMENSION);
|
||||||
|
|
||||||
newdim[0] = x->geometry[0];
|
newdim[0] = x->geometry[0];
|
||||||
newdim[1] = y->geometry[1];
|
newdim[1] = y->geometry[1];
|
||||||
Var_new(&foo, thisType, 2, newdim, 0);
|
Var_new(&foo, thisType, 2, newdim, 0);
|
||||||
for (i=unused; i<newdim[0]; ++i) for (j=unused; j<newdim[1]; ++j)
|
for (i = unused; i < newdim[0]; ++i)
|
||||||
|
for (j = unused; j < newdim[1]; ++j)
|
||||||
{
|
{
|
||||||
struct Value *dp = &foo.value[i * newdim[1] + j];
|
struct Value *dp = &foo.value[i * newdim[1] + j];
|
||||||
|
|
||||||
@ -245,6 +288,7 @@ struct Value *Var_mat_mult(struct Var *this, struct Var *x, struct Var *y, struc
|
|||||||
}
|
}
|
||||||
Value_retype(dp, thisType);
|
Value_retype(dp, thisType);
|
||||||
}
|
}
|
||||||
|
|
||||||
Var_destroy(this);
|
Var_destroy(this);
|
||||||
*this = foo;
|
*this = foo;
|
||||||
}
|
}
|
||||||
@ -252,13 +296,16 @@ struct Value *Var_mat_mult(struct Var *this, struct Var *x, struct Var *y, struc
|
|||||||
{
|
{
|
||||||
Value_clone(err, x->value);
|
Value_clone(err, x->value);
|
||||||
Value_mult(err, y->value, 0);
|
Value_mult(err, y->value, 0);
|
||||||
if (err->type==V_ERROR) return err;
|
if (err->type == V_ERROR)
|
||||||
|
return err;
|
||||||
|
|
||||||
Value_destroy(err);
|
Value_destroy(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (struct Value *)0;
|
return (struct Value *)0;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
|
||||||
struct Value *Var_mat_scalarMult(struct Var *this, struct Value *factor, struct Var *x, int work) /*{{{*/
|
struct Value *Var_mat_scalarMult(struct Var *this, struct Value *factor, struct Var *x, int work)
|
||||||
{
|
{
|
||||||
enum ValueType thisType = this->type;
|
enum ValueType thisType = this->type;
|
||||||
|
|
||||||
@ -275,9 +322,11 @@ struct Value *Var_mat_scalarMult(struct Var *this, struct Value *factor, struct
|
|||||||
Var_destroy(this);
|
Var_destroy(this);
|
||||||
Var_new(this, thisType, x->dim, x->geometry, 0);
|
Var_new(this, thisType, x->dim, x->geometry, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
g0 = x->geometry[0];
|
g0 = x->geometry[0];
|
||||||
g1 = x->dim == 1 ? unused + 1 : x->geometry[1];
|
g1 = x->dim == 1 ? unused + 1 : x->geometry[1];
|
||||||
for (i=unused; i<g0; ++i) for (j=unused; j<g1; ++j)
|
for (i = unused; i < g0; ++i)
|
||||||
|
for (j = unused; j < g1; ++j)
|
||||||
{
|
{
|
||||||
unsigned int element = x->dim == 1 ? i : i * g1 + j;
|
unsigned int element = x->dim == 1 ? i : i * g1 + j;
|
||||||
struct Value foo;
|
struct Value foo;
|
||||||
@ -290,18 +339,21 @@ struct Value *Var_mat_scalarMult(struct Var *this, struct Value *factor, struct
|
|||||||
*factor = foo;
|
*factor = foo;
|
||||||
return factor;
|
return factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value_destroy(&(this->value[element]));
|
Value_destroy(&(this->value[element]));
|
||||||
this->value[element] = *Value_retype(&foo, thisType);
|
this->value[element] = *Value_retype(&foo, thisType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (Value_mult(factor,this->value,0)->type==V_ERROR) return factor;
|
if (Value_mult(factor, this->value, 0)->type == V_ERROR)
|
||||||
|
return factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (struct Value *)0;
|
return (struct Value *)0;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
|
||||||
void Var_mat_transpose(struct Var *this, struct Var *x) /*{{{*/
|
void Var_mat_transpose(struct Var *this, struct Var *x)
|
||||||
{
|
{
|
||||||
unsigned int geometry[2];
|
unsigned int geometry[2];
|
||||||
enum ValueType thisType = this->type;
|
enum ValueType thisType = this->type;
|
||||||
@ -311,75 +363,117 @@ void Var_mat_transpose(struct Var *this, struct Var *x) /*{{{*/
|
|||||||
geometry[0] = x->geometry[1];
|
geometry[0] = x->geometry[1];
|
||||||
geometry[1] = x->geometry[0];
|
geometry[1] = x->geometry[0];
|
||||||
Var_new(&foo, thisType, 2, geometry, 0);
|
Var_new(&foo, thisType, 2, geometry, 0);
|
||||||
for (i=0; i<x->geometry[0]; ++i) for (j=0; j<x->geometry[1]; ++j)
|
for (i = 0; i < x->geometry[0]; ++i)
|
||||||
|
for (j = 0; j < x->geometry[1]; ++j)
|
||||||
{
|
{
|
||||||
Value_destroy(&foo.value[j * x->geometry[0] + i]);
|
Value_destroy(&foo.value[j * x->geometry[0] + i]);
|
||||||
Value_clone(&foo.value[j*x->geometry[0]+i],&(x->value[i*x->geometry[1]+j]));
|
Value_clone(&foo.value[j * x->geometry[0] + i],
|
||||||
|
&(x->value[i * x->geometry[1] + j]));
|
||||||
Value_retype(&foo.value[j * x->geometry[0] + i], thisType);
|
Value_retype(&foo.value[j * x->geometry[0] + i], thisType);
|
||||||
}
|
}
|
||||||
|
|
||||||
Var_destroy(this);
|
Var_destroy(this);
|
||||||
*this = foo;
|
*this = foo;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
|
||||||
struct Value *Var_mat_invert(struct Var *this, struct Var *x, struct Value *det, struct Value *err) /*{{{*/
|
struct Value *Var_mat_invert(struct Var *this, struct Var *x, struct Value *det, struct Value *err)
|
||||||
{
|
{
|
||||||
enum ValueType thisType = this->type;
|
enum ValueType thisType = this->type;
|
||||||
int n, i, j, k, max;
|
int n, i, j, k, max;
|
||||||
double t, *a, *u, d;
|
double t, *a, *u, d;
|
||||||
int unused = 1 - x->base;
|
int unused = 1 - x->base;
|
||||||
|
|
||||||
if (x->type!=V_INTEGER && x->type!=V_REAL) return Value_new_ERROR(err,TYPEMISMATCH5);
|
if (x->type != V_INTEGER && x->type != V_REAL)
|
||||||
|
return Value_new_ERROR(err, TYPEMISMATCH5);
|
||||||
|
|
||||||
assert(x->base == 0 || x->base == 1);
|
assert(x->base == 0 || x->base == 1);
|
||||||
if (x->geometry[0]!=x->geometry[1]) return Value_new_ERROR(err,DIMENSION);
|
if (x->geometry[0] != x->geometry[1])
|
||||||
|
return Value_new_ERROR(err, DIMENSION);
|
||||||
|
|
||||||
n = x->geometry[0] - unused;
|
n = x->geometry[0] - unused;
|
||||||
|
|
||||||
a = malloc(sizeof(double) * n * n);
|
a = malloc(sizeof(double) * n * n);
|
||||||
u = malloc(sizeof(double) * n * n);
|
u = malloc(sizeof(double) * n * n);
|
||||||
for (i=0; i<n; ++i) for (j=0; j<n; ++j)
|
for (i = 0; i < n; ++i)
|
||||||
|
for (j = 0; j < n; ++j)
|
||||||
{
|
{
|
||||||
if (x->type==V_INTEGER) a[i*n+j]=x->value[(i+unused)*(n+unused)+j+unused].u.integer;
|
if (x->type == V_INTEGER)
|
||||||
else a[i*n+j]=x->value[(i+unused)*(n+unused)+j+unused].u.real;
|
a[i * n + j] =
|
||||||
|
x->value[(i + unused) * (n + unused) + j + unused].u.integer;
|
||||||
|
else
|
||||||
|
a[i * n + j] =
|
||||||
|
x->value[(i + unused) * (n + unused) + j + unused].u.real;
|
||||||
u[i * n + j] = (i == j ? 1.0 : 0.0);
|
u[i * n + j] = (i == j ? 1.0 : 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
d = 1.0;
|
d = 1.0;
|
||||||
|
|
||||||
for (i=0; i<n; ++i) /* get zeroes in column i below the main diagonale */
|
for (i = 0; i < n; ++i) /* get zeroes in column i below the main
|
||||||
|
* diagonale */
|
||||||
{
|
{
|
||||||
max = i;
|
max = i;
|
||||||
for (j=i+1; j<n; ++j) if (fabs(a[j*n+i])>fabs(a[max*n+i])) max=j;
|
for (j = i + 1; j < n; ++j)
|
||||||
|
if (fabs(a[j * n + i]) > fabs(a[max * n + i]))
|
||||||
|
max = j;
|
||||||
|
|
||||||
/* exchanging row i against row max */
|
/* exchanging row i against row max */
|
||||||
if (i!=max) d=-d;
|
|
||||||
for (k=i; k<n; ++k) { t=a[i*n+k]; a[i*n+k]=a[max*n+k]; a[max*n+k]=t; }
|
if (i != max)
|
||||||
for (k=0; k<n; ++k) { t=u[i*n+k]; u[i*n+k]=u[max*n+k]; u[max*n+k]=t; }
|
d = -d;
|
||||||
|
|
||||||
|
for (k = i; k < n; ++k)
|
||||||
|
{
|
||||||
|
t = a[i * n + k];
|
||||||
|
a[i * n + k] = a[max * n + k];
|
||||||
|
a[max * n + k] = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (k = 0; k < n; ++k)
|
||||||
|
{
|
||||||
|
t = u[i * n + k];
|
||||||
|
u[i * n + k] = u[max * n + k];
|
||||||
|
u[max * n + k] = t;
|
||||||
|
}
|
||||||
|
|
||||||
if (a[i * n + i] == 0.0)
|
if (a[i * n + i] == 0.0)
|
||||||
{
|
{
|
||||||
free(a);
|
free(a);
|
||||||
free(u);
|
free(u);
|
||||||
return Value_new_ERROR(err, SINGULAR);
|
return Value_new_ERROR(err, SINGULAR);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = i + 1; j < n; ++j)
|
for (j = i + 1; j < n; ++j)
|
||||||
{
|
{
|
||||||
t = a[j * n + i] / a[i * n + i];
|
t = a[j * n + i] / a[i * n + i];
|
||||||
/* substract row i*t from row j */
|
/* substract row i*t from row j */
|
||||||
for (k=i; k<n; ++k) a[j*n+k]-=a[i*n+k]*t;
|
for (k = i; k < n; ++k)
|
||||||
for (k=0; k<n; ++k) u[j*n+k]-=u[i*n+k]*t;
|
a[j * n + k] -= a[i * n + k] * t;
|
||||||
|
for (k = 0; k < n; ++k)
|
||||||
|
u[j * n + k] -= u[i * n + k] * t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i=0; i<n; ++i) d*=a[i*n+i]; /* compute determinant */
|
for (i = 0; i < n; ++i)
|
||||||
|
d *= a[i * n + i]; /* compute determinant */
|
||||||
|
|
||||||
for (i=n-1; i>=0; --i) /* get zeroes in column i above the main diagonale */
|
for (i = n - 1; i >= 0; --i) /* get zeroes in column i above the main
|
||||||
|
* diagonale */
|
||||||
{
|
{
|
||||||
for (j = 0; j < i; ++j)
|
for (j = 0; j < i; ++j)
|
||||||
{
|
{
|
||||||
t = a[j * n + i] / a[i * n + i];
|
t = a[j * n + i] / a[i * n + i];
|
||||||
|
|
||||||
/* subtract row i*t from row j */
|
/* subtract row i*t from row j */
|
||||||
|
|
||||||
a[j * n + i] = 0.0; /* a[j*n+i]-=a[i*n+i]*t; */
|
a[j * n + i] = 0.0; /* a[j*n+i]-=a[i*n+i]*t; */
|
||||||
for (k=0; k<n; ++k) u[j*n+k]-=u[i*n+k]*t;
|
for (k = 0; k < n; ++k)
|
||||||
|
u[j * n + k] -= u[i * n + k] * t;
|
||||||
}
|
}
|
||||||
|
|
||||||
t = a[i * n + i];
|
t = a[i * n + i];
|
||||||
a[i * n + i] = 1.0; /* a[i*n+i]/=t; */
|
a[i * n + i] = 1.0; /* a[i*n+i]/=t; */
|
||||||
for (k=0; k<n; ++k) u[i*n+k]/=t;
|
for (k = 0; k < n; ++k)
|
||||||
|
u[i * n + k] /= t;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(a);
|
free(a);
|
||||||
@ -388,44 +482,70 @@ struct Value *Var_mat_invert(struct Var *this, struct Var *x, struct Value *det,
|
|||||||
Var_destroy(this);
|
Var_destroy(this);
|
||||||
Var_new(this, thisType, 2, x->geometry, x->base);
|
Var_new(this, thisType, 2, x->geometry, x->base);
|
||||||
}
|
}
|
||||||
for (i=0; i<n; ++i) for (j=0; j<n; ++j)
|
|
||||||
|
for (i = 0; i < n; ++i)
|
||||||
|
for (j = 0; j < n; ++j)
|
||||||
{
|
{
|
||||||
Value_destroy(&this->value[(i + unused) * (n + unused) + j + unused]);
|
Value_destroy(&this->value[(i + unused) * (n + unused) + j + unused]);
|
||||||
if (thisType==V_INTEGER) Value_new_INTEGER(&this->value[(i+unused)*(n+unused)+j+unused],u[i*n+j]);
|
if (thisType == V_INTEGER)
|
||||||
else Value_new_REAL(&this->value[(i+unused)*(n+unused)+j+unused],u[i*n+j]);
|
Value_new_INTEGER(&this->
|
||||||
|
value[(i + unused) * (n + unused) + j + unused],
|
||||||
|
u[i * n + j]);
|
||||||
|
else
|
||||||
|
Value_new_REAL(&this->value[(i + unused) * (n + unused) + j + unused],
|
||||||
|
u[i * n + j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(u);
|
free(u);
|
||||||
Value_destroy(det);
|
Value_destroy(det);
|
||||||
if (thisType==V_INTEGER) Value_new_INTEGER(det,d);
|
if (thisType == V_INTEGER)
|
||||||
else Value_new_REAL(det,d);
|
Value_new_INTEGER(det, d);
|
||||||
|
else
|
||||||
|
Value_new_REAL(det, d);
|
||||||
|
|
||||||
return (struct Value *)0;
|
return (struct Value *)0;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
|
||||||
struct Value *Var_mat_redim(struct Var *this, unsigned int dim, const unsigned int *geometry, struct Value *err) /*{{{*/
|
struct Value *Var_mat_redim(struct Var *this, unsigned int dim, const unsigned int *geometry, struct Value *err)
|
||||||
{
|
{
|
||||||
unsigned int i, j, size;
|
unsigned int i, j, size;
|
||||||
struct Value *value;
|
struct Value *value;
|
||||||
int unused = 1 - this->base;
|
int unused = 1 - this->base;
|
||||||
int g0, g1;
|
int g0, g1;
|
||||||
|
|
||||||
if (this->dim>0 && this->dim!=dim) return Value_new_ERROR(err,DIMENSION);
|
if (this->dim > 0 && this->dim != dim)
|
||||||
for (size=1,i=0; i<dim; ++i) size*=geometry[i];
|
return Value_new_ERROR(err, DIMENSION);
|
||||||
|
|
||||||
|
for (size = 1, i = 0; i < dim; ++i)
|
||||||
|
size *= geometry[i];
|
||||||
|
|
||||||
value = malloc(sizeof(struct Value) * size);
|
value = malloc(sizeof(struct Value) * size);
|
||||||
g0 = geometry[0];
|
g0 = geometry[0];
|
||||||
g1 = dim == 1 ? 1 : geometry[1];
|
g1 = dim == 1 ? 1 : geometry[1];
|
||||||
for (i=0; i<g0; ++i) for (j=0; j<g1; ++j)
|
for (i = 0; i < g0; ++i)
|
||||||
|
for (j = 0; j < g1; ++j)
|
||||||
{
|
{
|
||||||
if (this->dim==0 || i<unused || (dim==2 && j<unused) || i>=this->geometry[0] || (this->dim==2 && j>=this->geometry[1])) Value_new_null(&(value[i*g1+j]),this->type);
|
if (this->dim == 0 || i < unused || (dim == 2 && j < unused) ||
|
||||||
else Value_clone(&value[dim==1 ? i : i*g1+j],&this->value[dim==1 ? i : i*this->geometry[1]+j]);
|
i >= this->geometry[0] || (this->dim == 2 &&
|
||||||
|
j >= this->geometry[1]))
|
||||||
|
Value_new_null(&(value[i * g1 + j]), this->type);
|
||||||
|
else
|
||||||
|
Value_clone(&value[dim == 1 ? i : i * g1 + j],
|
||||||
|
&this->value[dim == 1 ? i : i * this->geometry[1] + j]);
|
||||||
}
|
}
|
||||||
for (i=0; i<this->size; ++i) Value_destroy(&this->value[i]);
|
|
||||||
|
for (i = 0; i < this->size; ++i)
|
||||||
|
Value_destroy(&this->value[i]);
|
||||||
|
|
||||||
free(this->value);
|
free(this->value);
|
||||||
if (this->geometry==(unsigned int*)0) this->geometry=malloc(sizeof(unsigned int)*dim);
|
if (this->geometry == (unsigned int *)0)
|
||||||
for (i=0; i<dim; ++i) this->geometry[i]=geometry[i];
|
this->geometry = malloc(sizeof(unsigned int) * dim);
|
||||||
|
|
||||||
|
for (i = 0; i < dim; ++i)
|
||||||
|
this->geometry[i] = geometry[i];
|
||||||
|
|
||||||
this->dim = dim;
|
this->dim = dim;
|
||||||
this->size = size;
|
this->size = size;
|
||||||
this->value = value;
|
this->value = value;
|
||||||
return (struct Value *)0;
|
return (struct Value *)0;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user