2014-11-04 21:00:01 +01:00
|
|
|
/****************************************************************************
|
2014-11-11 19:34:00 +01:00
|
|
|
* apps/interpreters/bas/bas_var.c
|
2014-11-04 21:00:01 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 1999-2014 Michael Haardt
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-02 01:18:06 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-03 16:11:25 +01:00
|
|
|
#include <nuttx/config.h>
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-11-11 19:34:00 +01:00
|
|
|
#include "bas_error.h"
|
|
|
|
#include "bas_var.h"
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-03 15:33:39 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define _(String) String
|
|
|
|
|
2014-11-02 01:18:06 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Var *Var_new(struct Var *self, enum ValueType type, unsigned int dim,
|
2014-11-04 21:00:01 +01:00
|
|
|
const unsigned int *geometry, int base)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
size_t newsize;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
self->type = type;
|
|
|
|
self->dim = dim;
|
|
|
|
self->base = base;
|
|
|
|
for (newsize = self->size = 1, dim = 0; dim < self->dim; ++dim)
|
2014-11-02 01:18:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
if ((newsize *= geometry[dim]) < self->size)
|
2014-11-02 01:18:06 +01:00
|
|
|
return (struct Var *)0;
|
2023-09-11 06:02:05 +02:00
|
|
|
self->size = newsize;
|
2014-11-02 01:18:06 +01:00
|
|
|
}
|
2014-11-04 21:00:01 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
if ((newsize *= sizeof(struct Value)) < self->size)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
return (struct Var *)0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
if ((self->value = malloc(newsize)) == (struct Value *)0)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
return (struct Var *)0;
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
if (dim)
|
2014-11-02 01:18:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->geometry = malloc(sizeof(unsigned int) * dim);
|
2014-11-02 01:18:06 +01:00
|
|
|
for (i = 0; i < dim; ++i)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->geometry[i] = geometry[i];
|
2014-11-04 21:00:01 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
else
|
2014-11-02 01:18:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->geometry = (unsigned int *)0;
|
2014-11-02 01:18:06 +01:00
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
for (i = 0; i < self->size; ++i)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_new_null(&(self->value[i]), type);
|
2014-11-04 21:00:01 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Var *Var_new_scalar(struct Var *self)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->dim = 0;
|
|
|
|
self->size = 1;
|
|
|
|
self->geometry = (unsigned int *)0;
|
|
|
|
self->value = malloc(sizeof(struct Value));
|
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
void Var_destroy(struct Var *self)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
while (self->size--)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(&(self->value[self->size]));
|
2014-11-04 21:00:01 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
free(self->value);
|
|
|
|
self->value = (struct Value *)0;
|
|
|
|
self->size = 0;
|
|
|
|
self->dim = 0;
|
|
|
|
if (self->geometry)
|
2014-11-02 01:18:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
free(self->geometry);
|
|
|
|
self->geometry = (unsigned int *)0;
|
2014-11-02 01:18:06 +01:00
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
void Var_retype(struct Var *self, enum ValueType type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
for (i = 0; i < self->size; ++i)
|
2014-11-02 01:18:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(&(self->value[i]));
|
|
|
|
Value_new_null(&(self->value[i]), type);
|
2014-11-02 01:18:06 +01:00
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Var_value(struct Var *self, unsigned int dim, int idx[],
|
2014-11-04 21:00:01 +01:00
|
|
|
struct Value *value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
unsigned int offset;
|
|
|
|
unsigned int i;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(self->value);
|
|
|
|
if (dim != self->dim)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, DIMENSION);
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
|
|
|
for (offset = 0, i = 0; i < dim; ++i)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
if (idx[i] < self->base || (idx[i] - self->base) >= self->geometry[i])
|
2014-11-02 01:18:06 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, OUTOFRANGE, _("array index"));
|
|
|
|
}
|
2014-11-04 21:00:01 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
offset = offset * self->geometry[i] + (idx[i] - self->base);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(offset < self->size);
|
|
|
|
return self->value + offset;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
void Var_clear(struct Var *self)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
for (i = 0; i < self->size; ++i)
|
2014-11-02 01:18:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(&(self->value[i]));
|
2014-11-02 01:18:06 +01:00
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self->geometry)
|
2014-11-02 01:18:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
free(self->geometry);
|
|
|
|
self->geometry = (unsigned int *)0;
|
|
|
|
self->size = 1;
|
|
|
|
self->dim = 0;
|
2014-11-02 01:18:06 +01:00
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_new_null(&(self->value[0]), self->type);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Var_mat_assign(struct Var *self, struct Var *x, struct Value *err,
|
2014-11-04 21:00:01 +01:00
|
|
|
int work)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
enum ValueType thisType = self->type;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
if (work)
|
|
|
|
{
|
2014-11-02 01:18:06 +01:00
|
|
|
unsigned int i, j;
|
|
|
|
int unused = 1 - x->base;
|
|
|
|
int g0, g1;
|
|
|
|
|
|
|
|
assert(x->base == 0 || x->base == 1);
|
|
|
|
assert(x->dim == 1 || x->dim == 2);
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self == x)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
return (struct Value *)0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Var_destroy(self);
|
|
|
|
Var_new(self, thisType, x->dim, x->geometry, x->base);
|
2014-11-02 01:18:06 +01:00
|
|
|
g0 = x->geometry[0];
|
|
|
|
g1 = x->dim == 1 ? unused + 1 : x->geometry[1];
|
|
|
|
for (i = unused; i < g0; ++i)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
for (j = unused; j < g1; ++j)
|
|
|
|
{
|
|
|
|
unsigned int element = x->dim == 1 ? i : i * g1 + j;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(&(self->value[element]));
|
|
|
|
Value_clone(&(self->value[element]), &(x->value[element]));
|
|
|
|
Value_retype(&(self->value[element]), thisType);
|
2014-11-04 21:00:01 +01:00
|
|
|
}
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-02 01:18:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
if (Value_commonType[self->type][x->type] == V_ERROR)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
return Value_new_typeError(err, self->type, x->type);
|
2014-11-04 21:00:01 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (struct Value *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Var_mat_addsub(struct Var *self, struct Var *x, struct Var *y,
|
2014-11-04 21:00:01 +01:00
|
|
|
int add, struct Value *err, int work)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
enum ValueType thisType = self->type;
|
2014-11-02 01:18:06 +01:00
|
|
|
struct Value foo, bar;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
if (work)
|
|
|
|
{
|
2014-11-02 01:18:06 +01:00
|
|
|
unsigned int i, j;
|
|
|
|
int unused = 1 - x->base;
|
|
|
|
int g0, g1;
|
|
|
|
|
|
|
|
assert(x->base == 0 || x->base == 1);
|
|
|
|
assert(x->dim == 1 || x->dim == 2);
|
|
|
|
if (x->base != y->base || x->dim != y->dim ||
|
2014-11-04 21:00:01 +01:00
|
|
|
x->geometry[0] != y->geometry[0] ||
|
|
|
|
(x->dim == 2 && x->geometry[1] != y->geometry[1]))
|
|
|
|
{
|
|
|
|
return Value_new_ERROR(err, DIMENSION);
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self != x && self != y)
|
2014-11-02 01:18:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Var_destroy(self);
|
|
|
|
Var_new(self, thisType, x->dim, x->geometry, x->base);
|
2014-11-02 01:18:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g0 = x->geometry[0];
|
|
|
|
g1 = x->dim == 1 ? unused + 1 : x->geometry[1];
|
|
|
|
for (i = unused; i < g0; ++i)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
for (j = unused; j < g1; ++j)
|
|
|
|
{
|
|
|
|
unsigned int element = x->dim == 1 ? i : i * g1 + j;
|
|
|
|
|
|
|
|
Value_clone(&foo, &(x->value[element]));
|
|
|
|
Value_clone(&bar, &(y->value[element]));
|
|
|
|
if (add)
|
|
|
|
{
|
|
|
|
Value_add(&foo, &bar, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Value_sub(&foo, &bar, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foo.type == V_ERROR)
|
|
|
|
{
|
|
|
|
*err = foo;
|
|
|
|
Value_destroy(&bar);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value_destroy(&bar);
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(&(self->value[element]));
|
|
|
|
self->value[element] = *Value_retype(&foo, thisType);
|
2014-11-04 21:00:01 +01:00
|
|
|
}
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
else
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:18:06 +01:00
|
|
|
Value_clone(err, x->value);
|
|
|
|
if (add)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
Value_add(err, y->value, 0);
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
else
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
Value_sub(err, y->value, 0);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:18:06 +01:00
|
|
|
if (err->type == V_ERROR)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
|
|
|
Value_destroy(err);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
|
|
|
return (struct Value *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Var_mat_mult(struct Var *self, struct Var *x, struct Var *y,
|
2014-11-04 21:00:01 +01:00
|
|
|
struct Value *err, int work)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
enum ValueType thisType = self->type;
|
2014-10-27 14:53:12 +01:00
|
|
|
struct Var foo;
|
|
|
|
|
|
|
|
if (work)
|
|
|
|
{
|
2014-11-02 01:18:06 +01:00
|
|
|
unsigned int newdim[2];
|
|
|
|
unsigned int i, j, k;
|
|
|
|
int unused = 1 - x->base;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:18:06 +01:00
|
|
|
assert(x->base == 0 || x->base == 1);
|
|
|
|
if (x->dim != 2 || y->dim != 2 || x->base != y->base ||
|
|
|
|
x->geometry[1] != y->geometry[0])
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(err, DIMENSION);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:18:06 +01:00
|
|
|
newdim[0] = x->geometry[0];
|
|
|
|
newdim[1] = y->geometry[1];
|
|
|
|
Var_new(&foo, thisType, 2, newdim, 0);
|
|
|
|
for (i = unused; i < newdim[0]; ++i)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
for (j = unused; j < newdim[1]; ++j)
|
|
|
|
{
|
|
|
|
struct Value *dp = &foo.value[i * newdim[1] + j];
|
|
|
|
|
|
|
|
Value_new_null(dp, thisType);
|
|
|
|
for (k = unused; k < x->geometry[1]; ++k)
|
|
|
|
{
|
|
|
|
struct Value p;
|
|
|
|
|
|
|
|
Value_clone(&p, &(x->value[i * x->geometry[1] + k]));
|
|
|
|
Value_mult(&p, &(y->value[k * y->geometry[1] + j]), 1);
|
|
|
|
if (p.type == V_ERROR)
|
|
|
|
{
|
|
|
|
*err = p;
|
|
|
|
Var_destroy(&foo);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value_add(dp, &p, 1);
|
|
|
|
Value_destroy(&p);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value_retype(dp, thisType);
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Var_destroy(self);
|
|
|
|
*self = foo;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-02 01:18:06 +01:00
|
|
|
{
|
|
|
|
Value_clone(err, x->value);
|
|
|
|
Value_mult(err, y->value, 0);
|
|
|
|
if (err->type == V_ERROR)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
|
|
|
Value_destroy(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (struct Value *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Var_mat_scalarMult(struct Var *self, struct Value *factor,
|
2014-11-04 21:00:01 +01:00
|
|
|
struct Var *x, int work)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
enum ValueType thisType = self->type;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
if (work)
|
|
|
|
{
|
2014-11-02 01:18:06 +01:00
|
|
|
unsigned int i, j;
|
|
|
|
int unused = 1 - x->base;
|
|
|
|
int g0, g1;
|
|
|
|
|
|
|
|
assert(x->base == 0 || x->base == 1);
|
|
|
|
assert(x->dim == 1 || x->dim == 2);
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self != x)
|
2014-11-02 01:18:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Var_destroy(self);
|
|
|
|
Var_new(self, thisType, x->dim, x->geometry, 0);
|
2014-11-02 01:18:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g0 = x->geometry[0];
|
|
|
|
g1 = x->dim == 1 ? unused + 1 : x->geometry[1];
|
|
|
|
for (i = unused; i < g0; ++i)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
for (j = unused; j < g1; ++j)
|
|
|
|
{
|
|
|
|
unsigned int element = x->dim == 1 ? i : i * g1 + j;
|
|
|
|
struct Value foo;
|
|
|
|
|
|
|
|
Value_clone(&foo, &(x->value[element]));
|
|
|
|
Value_mult(&foo, factor, 1);
|
|
|
|
if (foo.type == V_ERROR)
|
|
|
|
{
|
|
|
|
Value_destroy(factor);
|
|
|
|
*factor = foo;
|
|
|
|
return factor;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(&(self->value[element]));
|
|
|
|
self->value[element] = *Value_retype(&foo, thisType);
|
2014-11-04 21:00:01 +01:00
|
|
|
}
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
else
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
if (Value_mult(factor, self->value, 0)->type == V_ERROR)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
return factor;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
|
|
|
return (struct Value *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
void Var_mat_transpose(struct Var *self, struct Var *x)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
unsigned int geometry[2];
|
2023-09-11 06:02:05 +02:00
|
|
|
enum ValueType thisType = self->type;
|
2014-11-02 01:18:06 +01:00
|
|
|
unsigned int i, j;
|
2014-10-27 14:53:12 +01:00
|
|
|
struct Var foo;
|
|
|
|
|
2014-11-02 01:18:06 +01:00
|
|
|
geometry[0] = x->geometry[1];
|
|
|
|
geometry[1] = x->geometry[0];
|
|
|
|
Var_new(&foo, thisType, 2, geometry, 0);
|
|
|
|
for (i = 0; i < x->geometry[0]; ++i)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
for (j = 0; j < x->geometry[1]; ++j)
|
|
|
|
{
|
|
|
|
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_retype(&foo.value[j * x->geometry[0] + i], thisType);
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Var_destroy(self);
|
|
|
|
*self = foo;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Var_mat_invert(struct Var *self, struct Var *x, struct Value *det,
|
2014-11-04 21:00:01 +01:00
|
|
|
struct Value *err)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
enum ValueType thisType = self->type;
|
2014-11-02 01:18:06 +01:00
|
|
|
int n, i, j, k, max;
|
|
|
|
double t, *a, *u, d;
|
|
|
|
int unused = 1 - x->base;
|
|
|
|
|
|
|
|
if (x->type != V_INTEGER && x->type != V_REAL)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(err, TYPEMISMATCH5);
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
|
|
|
assert(x->base == 0 || x->base == 1);
|
|
|
|
if (x->geometry[0] != x->geometry[1])
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(err, DIMENSION);
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
|
|
|
n = x->geometry[0] - unused;
|
|
|
|
|
|
|
|
a = malloc(sizeof(double) * n * n);
|
|
|
|
u = malloc(sizeof(double) * n * n);
|
|
|
|
for (i = 0; i < n; ++i)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
|
|
|
d = 1.0;
|
|
|
|
|
|
|
|
for (i = 0; i < n; ++i) /* get zeroes in column i below the main
|
2014-11-04 21:00:01 +01:00
|
|
|
* diagonal */
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:18:06 +01:00
|
|
|
max = i;
|
|
|
|
for (j = i + 1; j < n; ++j)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
if (fabs(a[j * n + i]) > fabs(a[max * n + i]))
|
|
|
|
{
|
|
|
|
max = j;
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
|
|
|
/* exchanging row i against row max */
|
|
|
|
|
|
|
|
if (i != max)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
d = -d;
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
free(a);
|
|
|
|
free(u);
|
|
|
|
return Value_new_ERROR(err, SINGULAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = i + 1; j < n; ++j)
|
|
|
|
{
|
|
|
|
t = a[j * n + i] / a[i * n + i];
|
2014-11-04 21:00:01 +01:00
|
|
|
|
|
|
|
/* Subtract row i*t from row j */
|
|
|
|
|
2014-11-02 01:18:06 +01:00
|
|
|
for (k = i; k < n; ++k)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
a[j * n + k] -= a[i * n + k] * t;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:18:06 +01:00
|
|
|
for (k = 0; k < n; ++k)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
u[j * n + k] -= u[i * n + k] * t;
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
|
2014-11-02 01:18:06 +01:00
|
|
|
for (i = 0; i < n; ++i)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
d *= a[i * n + i]; /* compute determinant */
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-04 21:00:01 +01:00
|
|
|
for (i = n - 1; i >= 0; --i) /* get zeroes in column i above the main diagonal */
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:18:06 +01:00
|
|
|
for (j = 0; j < i; ++j)
|
|
|
|
{
|
|
|
|
t = a[j * n + i] / a[i * n + i];
|
|
|
|
|
2014-11-04 21:00:01 +01:00
|
|
|
/* Subtract row i*t from row j */
|
2014-11-02 01:18:06 +01:00
|
|
|
|
|
|
|
a[j * n + i] = 0.0; /* a[j*n+i]-=a[i*n+i]*t; */
|
|
|
|
for (k = 0; k < n; ++k)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
u[j * n + k] -= u[i * n + k] * t;
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
t = a[i * n + i];
|
|
|
|
a[i * n + i] = 1.0; /* a[i*n+i]/=t; */
|
|
|
|
for (k = 0; k < n; ++k)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
u[i * n + k] /= t;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(a);
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self != x)
|
2014-11-02 01:18:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Var_destroy(self);
|
|
|
|
Var_new(self, thisType, 2, x->geometry, x->base);
|
2014-11-02 01:18:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < n; ++i)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
for (j = 0; j < n; ++j)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(&self->value[(i + unused) * (n + unused) + j + unused]);
|
2014-11-04 21:00:01 +01:00
|
|
|
if (thisType == V_INTEGER)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_new_INTEGER(&self->value
|
2014-11-04 21:00:01 +01:00
|
|
|
[(i + unused) * (n + unused) + j + unused],
|
|
|
|
u[i * n + j]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_new_REAL(&self->
|
2014-11-04 21:00:01 +01:00
|
|
|
value[(i + unused) * (n + unused) + j + unused],
|
|
|
|
u[i * n + j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
free(u);
|
|
|
|
Value_destroy(det);
|
2014-11-02 01:18:06 +01:00
|
|
|
if (thisType == V_INTEGER)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
Value_new_INTEGER(det, d);
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
else
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
Value_new_REAL(det, d);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:18:06 +01:00
|
|
|
return (struct Value *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Var_mat_redim(struct Var *self, unsigned int dim,
|
2014-11-04 21:00:01 +01:00
|
|
|
const unsigned int *geometry, struct Value *err)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:18:06 +01:00
|
|
|
unsigned int i, j, size;
|
2014-10-27 14:53:12 +01:00
|
|
|
struct Value *value;
|
2023-09-11 06:02:05 +02:00
|
|
|
int unused = 1 - self->base;
|
2014-11-02 01:18:06 +01:00
|
|
|
int g0, g1;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self->dim > 0 && self->dim != dim)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(err, DIMENSION);
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
|
|
|
for (size = 1, i = 0; i < dim; ++i)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
size *= geometry[i];
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
|
|
|
value = malloc(sizeof(struct Value) * size);
|
|
|
|
g0 = geometry[0];
|
|
|
|
g1 = dim == 1 ? 1 : geometry[1];
|
|
|
|
for (i = 0; i < g0; ++i)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
|
|
|
for (j = 0; j < g1; ++j)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self->dim == 0 || i < unused || (dim == 2 && j < unused) ||
|
|
|
|
i >= self->geometry[0] || (self->dim == 2 &&
|
|
|
|
j >= self->geometry[1]))
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_new_null(&(value[i * g1 + j]), self->type);
|
2014-11-04 21:00:01 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Value_clone(&value[dim == 1 ? i : i * g1 + j],
|
2023-09-11 06:02:05 +02:00
|
|
|
&self->value[dim ==
|
|
|
|
1 ? i : i * self->geometry[1] + j]);
|
2014-11-04 21:00:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
for (i = 0; i < self->size; ++i)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(&self->value[i]);
|
2014-11-04 21:00:01 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
free(self->value);
|
|
|
|
if (self->geometry == (unsigned int *)0)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->geometry = malloc(sizeof(unsigned int) * dim);
|
2014-11-04 21:00:01 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
|
|
|
for (i = 0; i < dim; ++i)
|
2014-11-04 21:00:01 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->geometry[i] = geometry[i];
|
2014-11-04 21:00:01 +01:00
|
|
|
}
|
2014-11-02 01:18:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
self->dim = dim;
|
|
|
|
self->size = size;
|
|
|
|
self->value = value;
|
2014-11-02 01:18:06 +01:00
|
|
|
return (struct Value *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|