aboutsummaryrefslogtreecommitdiff
path: root/externals/gridflow/base/flow_objects_for_matrix.c
blob: b88accba6f8e3c0c643aa282a87e6f1777eae4a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
	$Id: flow_objects_for_matrix.c,v 1.2 2006-03-15 04:37:08 matju Exp $

	GridFlow
	Copyright (c) 2001,2002,2003 by Mathieu Bouchard

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	See file ../COPYING for further informations on licensing terms.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include <math.h>
#include "grid.h.fcs"

// produce an upper triangular matrix with ones on the diagonal
// will also affect any additional columns using the same row-operations

void expect_complete_matrix (P<Dim> d) {
	if (d->n!=2) RAISE("bletch");
	if (d->get(0)>d->get(1)) RAISE("argh");
}

\class GridMatrixSolve < GridObject
struct GridMatrixSolve : GridObject {
	Numop *op_sub;
	Numop *op_mul;
	Numop *op_div;
	PtrGrid matrix;
	GridMatrixSolve() {
		matrix.constrain(expect_complete_matrix);
	}
	\decl void initialize ();
	\grin 0 float
};

GRID_INPUT(GridMatrixSolve,0,matrix) {
	int n = matrix->dim->get(0); // # rows
	int m = matrix->dim->get(1); // # columns
	Pt<T> mat = (Pt<T>)*matrix;
	for (int j=0; j<n; j++) {
		op_div->map(m,mat+j*m,mat[j*m+j]);
		for (int i=j+1; i<n; i++) {
			STACK_ARRAY(T,row,m);
			COPY(row,mat+j,m);
			op_mul->map(m,row,mat[i*m+j]);
			op_sub->zip(m,mat+i*m,row);
		}
	}
	GridOutlet out(this,0,matrix->dim);
	out.send(n*m,mat);
} GRID_END

\def void initialize () {
	rb_call_super(argc,argv);
	this->op_sub = op_sub;
	this->op_mul = op_mul;
	this->op_div = op_div;
}

\classinfo { IEVAL(rself,"install '#matrix_solve',1,1"); }
\end class

void startup_flow_objects_for_matrix () {
	\startall
}