2023-05-18 15:34:05 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include "algebra.h"
|
|
|
|
#include "basisblade.h"
|
|
|
|
#include "basislayout.h"
|
|
|
|
#include "multivector.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
@implementation MultiVector
|
|
|
|
static MultiVector *new_mv (Algebra *algebra, BasisLayout *layout)
|
|
|
|
{
|
|
|
|
MultiVector *mv = [[[MultiVector alloc] init] autorelease];
|
|
|
|
mv.algebra = [algebra retain];
|
|
|
|
layout = layout ? layout : [algebra layout];
|
|
|
|
mv.layout = [layout retain];
|
|
|
|
mv.num_components = [layout num_components];
|
|
|
|
mv.components = obj_malloc (mv.num_components * sizeof (double));
|
|
|
|
return mv;
|
|
|
|
}
|
|
|
|
|
|
|
|
+(MultiVector *) new:(Algebra *) algebra
|
|
|
|
{
|
|
|
|
MultiVector *mv = new_mv (algebra, nil);
|
|
|
|
for (int i = 0; i < mv.num_components; i++) {
|
|
|
|
mv.components[i] = 0;
|
|
|
|
}
|
|
|
|
return mv;
|
|
|
|
}
|
|
|
|
|
|
|
|
+(MultiVector *) new:(Algebra *) algebra values:(double *) values
|
|
|
|
{
|
|
|
|
MultiVector *mv = new_mv (algebra, nil);
|
|
|
|
for (int i = 0; i < mv.num_components; i++) {
|
|
|
|
mv.components[i] = values[i];
|
|
|
|
}
|
|
|
|
return mv;
|
|
|
|
}
|
|
|
|
|
|
|
|
+(MultiVector *) new:(Algebra *) algebra group:(BasisGroup *) group
|
|
|
|
{
|
|
|
|
BasisLayout *layout = [BasisLayout new:1 groups:&group];
|
|
|
|
MultiVector *mv = new_mv (algebra, layout);
|
|
|
|
for (int i = 0; i < mv.num_components; i++) {
|
|
|
|
mv.components[i] = 0;
|
|
|
|
}
|
|
|
|
return mv;
|
|
|
|
}
|
|
|
|
|
|
|
|
+(MultiVector *) new:(Algebra *) algebra group:(BasisGroup *) group values:(double *) values
|
|
|
|
{
|
|
|
|
BasisLayout *layout = [BasisLayout new:1 groups:&group];
|
|
|
|
MultiVector *mv = new_mv (algebra, layout);
|
|
|
|
for (int i = 0; i < mv.num_components; i++) {
|
|
|
|
mv.components[i] = values[i];
|
|
|
|
}
|
|
|
|
return mv;
|
|
|
|
}
|
|
|
|
|
|
|
|
+(MultiVector *) copy:(MultiVector *) src
|
|
|
|
{
|
|
|
|
MultiVector *mv = new_mv (src.algebra, src.layout);
|
|
|
|
for (int i = 0; i < mv.num_components; i++) {
|
|
|
|
mv.components[i] = src.components[i];
|
|
|
|
}
|
|
|
|
return mv;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)dealloc
|
|
|
|
{
|
|
|
|
[algebra release];
|
|
|
|
[layout release];
|
|
|
|
obj_free (components);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(string)describe
|
|
|
|
{
|
|
|
|
string str = nil;
|
|
|
|
|
|
|
|
for (int i = 0; i < num_components; i++) {
|
|
|
|
if (!components[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
BasisBlade *b = [layout bladeAt:i];
|
|
|
|
str = sprintf ("%s%s%g%s", str, str ? " + " : "", components[i], [b name]);
|
|
|
|
}
|
|
|
|
if (!str) {
|
|
|
|
str = "0";
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2023-05-27 03:39:13 +00:00
|
|
|
-(double *) components
|
|
|
|
{
|
|
|
|
return components;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(int)indexFor:(unsigned)mask
|
|
|
|
{
|
|
|
|
return [layout bladeIndex:mask];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(double *) componentFor:(BasisBlade *) blade
|
|
|
|
{
|
|
|
|
return &components[[layout bladeIndex:[blade mask]]];
|
|
|
|
}
|
|
|
|
|
2023-05-18 15:34:05 +00:00
|
|
|
-(MultiVector *) product:(MultiVector *) rhs
|
|
|
|
{
|
2023-06-08 04:00:32 +00:00
|
|
|
MultiVector *prod = [MultiVector new:algebra];
|
2023-05-18 15:34:05 +00:00
|
|
|
for (int i = 0; i < num_components; i++) {
|
|
|
|
if (!components[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
double lc = components[i];
|
|
|
|
BasisBlade *lb = [layout bladeAt:i];
|
|
|
|
for (int j = 0; j < rhs.num_components; j++) {
|
|
|
|
if (!rhs.components[j]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
double rc = rhs.components[j];
|
|
|
|
BasisBlade *rb = [rhs.layout bladeAt:j];
|
|
|
|
BasisBlade *b = [lb geometricProduct:rb metric:[algebra metric]];
|
|
|
|
double s = [b scale];
|
|
|
|
if (!s) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-05-19 02:15:29 +00:00
|
|
|
int ind = [prod.layout bladeIndex:[b mask]];
|
2023-05-18 15:34:05 +00:00
|
|
|
prod.components[ind] += s * lc * rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prod;
|
|
|
|
}
|
|
|
|
|
2023-05-29 00:52:10 +00:00
|
|
|
-(MultiVector *) divide:(MultiVector *) rhs
|
|
|
|
{
|
|
|
|
MultiVector *sqr = [rhs product:rhs];
|
|
|
|
double smag = sqr.components[[sqr indexFor:0]];
|
|
|
|
MultiVector *div = [self product:rhs];
|
|
|
|
for (int i = 0; i < div.num_components; i++) {
|
|
|
|
div.components[i] /= smag;
|
|
|
|
}
|
|
|
|
return div;
|
|
|
|
}
|
|
|
|
|
2023-05-18 15:34:05 +00:00
|
|
|
-(MultiVector *) wedge:(MultiVector *) rhs
|
|
|
|
{
|
2023-06-08 04:00:32 +00:00
|
|
|
MultiVector *prod = [MultiVector new:algebra];
|
2023-05-18 15:34:05 +00:00
|
|
|
for (int i = 0; i < num_components; i++) {
|
|
|
|
if (!components[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
double lc = components[i];
|
|
|
|
BasisBlade *lb = [layout bladeAt:i];
|
|
|
|
for (int j = 0; j < rhs.num_components; j++) {
|
|
|
|
if (!rhs.components[j]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
double rc = rhs.components[j];
|
|
|
|
BasisBlade *rb = [rhs.layout bladeAt:j];
|
|
|
|
BasisBlade *b = [lb outerProduct:rb];
|
|
|
|
double s = [b scale];
|
|
|
|
if (!s) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-05-19 02:15:29 +00:00
|
|
|
int ind = [prod.layout bladeIndex:[b mask]];
|
2023-05-18 15:34:05 +00:00
|
|
|
prod.components[ind] += s * lc * rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prod;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(MultiVector *) dot:(MultiVector *) rhs
|
|
|
|
{
|
2023-06-08 04:00:32 +00:00
|
|
|
MultiVector *prod = [MultiVector new:algebra];
|
2023-05-18 15:34:05 +00:00
|
|
|
for (int i = 0; i < num_components; i++) {
|
|
|
|
if (!components[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
double lc = components[i];
|
|
|
|
BasisBlade *lb = [layout bladeAt:i];
|
|
|
|
int lg = [lb grade];
|
|
|
|
for (int j = 0; j < rhs.num_components; j++) {
|
|
|
|
if (!rhs.components[j]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
double rc = rhs.components[j];
|
|
|
|
BasisBlade *rb = [rhs.layout bladeAt:j];
|
|
|
|
int rg = [rb grade];
|
2023-05-22 04:03:35 +00:00
|
|
|
BasisBlade *b = [lb geometricProduct:rb metric:[algebra metric]];
|
2023-05-18 15:34:05 +00:00
|
|
|
int g = [b grade];
|
|
|
|
if ((lg <= rg) && (g != rg - lg)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((lg > rg) && (g != lg - rg)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
double s = [b scale];
|
|
|
|
if (!s) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-05-19 02:15:29 +00:00
|
|
|
int ind = [prod.layout bladeIndex:[b mask]];
|
2023-05-18 15:34:05 +00:00
|
|
|
prod.components[ind] += s * lc * rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prod;
|
|
|
|
}
|
|
|
|
|
2023-05-27 03:39:13 +00:00
|
|
|
-(MultiVector *) plus:(MultiVector *) rhs
|
|
|
|
{
|
2023-06-08 04:00:32 +00:00
|
|
|
MultiVector *plus = [MultiVector new:algebra];
|
2023-05-27 03:39:13 +00:00
|
|
|
for (int i = 0; i < num_components; i++) {
|
|
|
|
double c = components[i];
|
|
|
|
if (!c) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
BasisBlade *b = [layout bladeAt:i];
|
|
|
|
int ind = [plus.layout bladeIndex:[b mask]];
|
|
|
|
plus.components[ind] += c;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < rhs.num_components; i++) {
|
|
|
|
double c = rhs.components[i];
|
|
|
|
if (!c) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
BasisBlade *b = [rhs.layout bladeAt:i];
|
|
|
|
int ind = [plus.layout bladeIndex:[b mask]];
|
|
|
|
plus.components[ind] += c;
|
|
|
|
}
|
|
|
|
return plus;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(MultiVector *) minus:(MultiVector *) rhs
|
|
|
|
{
|
2023-06-08 04:00:32 +00:00
|
|
|
MultiVector *minus = [MultiVector new:algebra];
|
2023-05-27 03:39:13 +00:00
|
|
|
for (int i = 0; i < num_components; i++) {
|
|
|
|
double c = components[i];
|
|
|
|
if (!c) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
BasisBlade *b = [layout bladeAt:i];
|
|
|
|
int ind = [minus.layout bladeIndex:[b mask]];
|
|
|
|
minus.components[ind] += c;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < rhs.num_components; i++) {
|
|
|
|
double c = rhs.components[i];
|
|
|
|
if (!c) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
BasisBlade *b = [rhs.layout bladeAt:i];
|
|
|
|
int ind = [minus.layout bladeIndex:[b mask]];
|
|
|
|
minus.components[ind] -= c;
|
|
|
|
}
|
|
|
|
return minus;
|
|
|
|
}
|
|
|
|
|
2023-05-18 15:34:05 +00:00
|
|
|
-(MultiVector *) dual
|
|
|
|
{
|
2023-06-08 04:00:32 +00:00
|
|
|
MultiVector *dual = [MultiVector new:algebra];
|
2023-05-18 15:34:05 +00:00
|
|
|
unsigned dual_mask = (1 << [algebra dimension]) - 1;
|
|
|
|
for (int i = 0; i < num_components; i++) {
|
|
|
|
if (!components[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
double lc = components[i];
|
|
|
|
BasisBlade *lb = [layout bladeAt:i];
|
2023-06-08 04:02:13 +00:00
|
|
|
unsigned lb_mask = [lb mask];
|
|
|
|
unsigned mask = lb_mask ^ dual_mask;
|
|
|
|
double ls = [lb scale];
|
|
|
|
double s = count_flips (lb_mask, mask) & 1 ? -1 : 1;
|
|
|
|
int ind = [dual.layout bladeIndex:mask];
|
2023-05-18 15:34:05 +00:00
|
|
|
dual.components[ind] += s * lc;
|
|
|
|
}
|
|
|
|
return dual;
|
|
|
|
}
|
2023-05-24 09:25:47 +00:00
|
|
|
|
|
|
|
-(MultiVector *) reverse
|
|
|
|
{
|
2023-06-08 04:00:32 +00:00
|
|
|
MultiVector *reverse = [MultiVector new:algebra];
|
2023-05-24 09:25:47 +00:00
|
|
|
for (int i = 0; i < num_components; i++) {
|
|
|
|
if (!components[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
double c = components[i];
|
|
|
|
BasisBlade *b = [layout bladeAt:i];
|
|
|
|
int g = [b grade];
|
|
|
|
unsigned mask = [b mask];
|
|
|
|
double s = g & 2 ? -1 : 1;//FIXME do in BasisBlade?
|
|
|
|
int ind = [layout bladeIndex:mask];
|
|
|
|
reverse.components[ind] += s * c;
|
|
|
|
}
|
|
|
|
return reverse;
|
|
|
|
}
|
2023-05-18 15:34:05 +00:00
|
|
|
@end
|