1 |
/* RCSid $Id: rmatrix.h,v 2.26 2025/04/04 18:06:48 greg Exp $ */ |
2 |
/* |
3 |
* Header file for general matrix routines. |
4 |
*/ |
5 |
|
6 |
#ifndef _RAD_RMATRIX_H_ |
7 |
#define _RAD_RMATRIX_H_ |
8 |
|
9 |
#include "cmatrix.h" |
10 |
|
11 |
#ifdef __cplusplus |
12 |
extern "C" { |
13 |
#endif |
14 |
|
15 |
/* RMATRIX flags (usually private): |
16 |
need to swap input, we should free memory */ |
17 |
#define RMF_SWAPIN 1 |
18 |
#define RMF_FREEMEM 2 |
19 |
|
20 |
#ifndef MAXCOMP |
21 |
#define MAXCOMP MAXCSAMP /* #components we support */ |
22 |
#endif |
23 |
/* Set in-core data type */ |
24 |
#if !defined(DTrmx_native) || DTrmx_native==DTfloat |
25 |
#define DTrmx_native DTfloat |
26 |
#define rmx_dtype float |
27 |
#define rmx_scanfmt "%f" |
28 |
#elif DTrmx_native==DTdouble |
29 |
#define rmx_dtype double |
30 |
#define rmx_scanfmt "%lf" |
31 |
#endif |
32 |
|
33 |
/* General [row][col][cmp] component matrix */ |
34 |
typedef struct { |
35 |
char *info; |
36 |
void *mapped; |
37 |
rmx_dtype *mtx; |
38 |
COLOR cexp; |
39 |
float wlpart[4]; |
40 |
int nrows, ncols; |
41 |
short ncomp; |
42 |
uby8 dtype; |
43 |
uby8 pflags; |
44 |
} RMATRIX; |
45 |
|
46 |
#define rmx_lval(rm,r,c) ((rm)->mtx + (rm)->ncomp*((c)+(size_t)(rm)->ncols*(r))) |
47 |
#define rmx_val rmx_lval |
48 |
|
49 |
#define rmx_array_size(rm) (sizeof(rmx_dtype)*(rm)->nrows*(rm)->ncols*(rm)->ncomp) |
50 |
#define rmx_mapped_size(rm) ((char *)(rm)->mtx + rmx_array_size(rm) - (char *)(rm)->mapped) |
51 |
|
52 |
/* Initialize a RMATRIX struct but don't allocate array space */ |
53 |
extern RMATRIX *rmx_new(int nr, int nc, int n); |
54 |
|
55 |
/* Prepare a RMATRIX for writing (allocate array if needed) */ |
56 |
extern int rmx_prepare(RMATRIX *rm); |
57 |
|
58 |
/* Call rmx_new() and rmx_prepare() */ |
59 |
extern RMATRIX *rmx_alloc(int nr, int nc, int n); |
60 |
|
61 |
/* Clear state by freeing info and matrix data */ |
62 |
extern void rmx_reset(RMATRIX *rm); |
63 |
|
64 |
/* Free an RMATRIX struct and data */ |
65 |
extern void rmx_free(RMATRIX *rm); |
66 |
|
67 |
/* Resolve data type based on two input types (returns 0 for mismatch) */ |
68 |
extern int rmx_newtype(int dtyp1, int dtyp2); |
69 |
|
70 |
/* Read matrix header from input stream (cannot be XML) */ |
71 |
extern int rmx_load_header(RMATRIX *rm, FILE *fp); |
72 |
|
73 |
/* Load next row as rmx_dtype (cannot be XML) */ |
74 |
extern int rmx_load_row(rmx_dtype *drp, const RMATRIX *rm, FILE *fp); |
75 |
|
76 |
/* Allocate & load post-header data from stream given type set in rm->dtype */ |
77 |
extern int rmx_load_data(RMATRIX *rm, FILE *fp); |
78 |
|
79 |
/* Load matrix from supported file type (NULL for stdin, '!' with command) */ |
80 |
extern RMATRIX *rmx_load(const char *inspec); |
81 |
|
82 |
/* Append header information associated with matrix data */ |
83 |
extern int rmx_addinfo(RMATRIX *rm, const char *info); |
84 |
|
85 |
/* Finish writing header data with resolution and format, returning type used */ |
86 |
extern int rmx_write_header(const RMATRIX *rm, int dtype, FILE *fp); |
87 |
|
88 |
/* Write out matrix data (usually by row) */ |
89 |
extern int rmx_write_data(const rmx_dtype *dp, int nc, int len, |
90 |
int dtype, FILE *fp); |
91 |
|
92 |
/* Write matrix using file format indicated by dtype */ |
93 |
extern int rmx_write(const RMATRIX *rm, int dtype, FILE *fp); |
94 |
|
95 |
/* Allocate and assign square identity matrix with n components */ |
96 |
extern RMATRIX *rmx_identity(int dim, int n); |
97 |
|
98 |
/* Duplicate the given matrix */ |
99 |
extern RMATRIX *rmx_copy(const RMATRIX *rm); |
100 |
|
101 |
/* Replace data in first matrix with data from second */ |
102 |
extern int rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int dometa); |
103 |
|
104 |
/* Transpose the given matrix */ |
105 |
extern int rmx_transpose(RMATRIX *rm); |
106 |
|
107 |
/* Multiply (concatenate) two matrices and allocate the result */ |
108 |
extern RMATRIX *rmx_multiply(const RMATRIX *m1, const RMATRIX *m2); |
109 |
|
110 |
/* Element-wise multiplication (or division) of m2 into m1 */ |
111 |
extern int rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide); |
112 |
|
113 |
/* Sum second matrix into first, applying scale factor beforehand */ |
114 |
extern int rmx_sum(RMATRIX *msum, const RMATRIX *madd, const double sf[]); |
115 |
|
116 |
/* Scale the given matrix by the indicated scalar component vector */ |
117 |
extern int rmx_scale(RMATRIX *rm, const double sf[]); |
118 |
|
119 |
/* Allocate new matrix and apply component transformation */ |
120 |
extern RMATRIX *rmx_transform(const RMATRIX *msrc, int n, const double cmat[]); |
121 |
|
122 |
/* Convert a color matrix to newly allocated RMATRIX buffer */ |
123 |
extern RMATRIX *rmx_from_cmatrix(const CMATRIX *cm); |
124 |
|
125 |
/* Convert general matrix to newly allocated CMATRIX buffer */ |
126 |
extern CMATRIX *cm_from_rmatrix(const RMATRIX *rm); |
127 |
|
128 |
#ifdef __cplusplus |
129 |
} |
130 |
#endif |
131 |
#endif /* _RAD_RMATRIX_H_ */ |