41 #include "EST_TMatrix.h"
45 #include "EST_matrix_support.h"
46 #include "EST_TVector.h"
47 #include "EST_cutils.h"
48 #include "EST_error.h"
83 T *memory,
int offset,
int free_when_destroyed)
86 set_memory(memory, offset, rows, cols, free_when_destroyed);
103 if (!EST_matrix_bounds_check(row, col, num_rows(), num_columns(), FALSE))
104 return *this->error_return;
106 return a_no_check(row,col);
130 int r_step,
int c_step,
131 int start_r,
int num_r,
132 int start_c,
int num_c
135 for(
int r=start_r, i=0, rp=0; i< num_r; i++, r++, rp+=r_step)
136 for(
int c=start_c, j=0, cp=0; j< num_c; j++, c++, cp+=c_step)
137 a_no_check(r,c) = data[rp+cp];
142 int r_step,
int c_step,
143 int start_r,
int num_r,
144 int start_c,
int num_c
147 for(
int r=start_r, i=0, rp=0; i< num_r; i++, r++, rp+=r_step)
148 for(
int c=start_c, j=0, cp=0; j< num_c; j++, c++, cp+=c_step)
149 data[rp+cp] = a_no_check(r,c);
170 EST_error(
"Can't add rows with differnet number of columns (%d vs %d)",
176 int old_num_rows = num_rows();
177 resize(num_rows()+in.
num_rows(), num_columns(), TRUE);
179 for(
int i=old_num_rows, i1=0; i<num_rows(); i++, i1++)
180 for(
int j=0; j<num_columns(); j++)
191 EST_error(
"Can't add columns with differnet number of rows (%d vs %d)",
197 int old_num_columns = num_columns();
198 resize(num_columns()+in.
num_columns(), num_rows(), TRUE);
200 for(
int i=old_num_columns, i1=0; i<num_columns(); i++, i1++)
201 for(
int j=0; j<num_rows(); j++)
215 if (num_rows() != new_rows || num_columns() != new_cols || this->p_memory == NULL )
217 if (this->p_sub_matrix)
218 EST_error(
"Attempt to resize Sub-Matrix");
220 if (new_cols < 0 || new_rows < 0)
221 EST_error(
"Attempt to resize matrix to negative size: %d x %d",
226 new_m =
new T[new_rows*new_cols];
228 if (this->p_memory != NULL)
230 if (old_vals != NULL)
231 *old_vals = this->p_memory;
232 else if (!this->p_sub_matrix)
233 delete [] (this->p_memory-this->p_offset);
236 p_num_rows = new_rows;
237 this->p_num_columns = new_cols;
239 p_row_step=this->p_num_columns;
240 this->p_column_step=1;
242 this->p_memory = new_m;
245 *old_vals = this->p_memory;
253 T * old_vals = this->p_memory;
254 int old_rows = num_rows();
255 int old_cols = num_columns();
256 int old_row_step = p_row_step;
257 int old_offset = this->p_offset;
258 int old_column_step = this->p_column_step;
265 just_resize(new_rows, new_cols, &old_vals);
272 if (old_vals != NULL)
274 copy_r = Lof(num_rows(), old_rows);
275 copy_c = Lof(num_columns(), old_cols);
278 old_row_step, old_column_step,
288 for(i=0; i<copy_r; i++)
289 for(j=copy_c; j<new_cols; j++)
290 a_no_check(i,j) = *this->def_val;
292 for(i=copy_r; i<new_rows; i++)
293 for(j=0; j<new_cols; j++)
294 a_no_check(i,j) = *this->def_val;
297 if (old_vals && old_vals != this->p_memory && !this->p_sub_matrix)
298 delete [] (old_vals-old_offset);
304 return this->p_offset >= n*p_row_step;
310 return this->p_offset >= n*this->p_column_step;
317 for (i = 0; i < num_rows(); ++i)
318 for (j = 0; j < num_columns(); ++j)
328 if (filename ==
"-" || filename ==
"")
331 outf =
new ofstream(filename);
333 for (i = 0; i < num_rows(); ++i)
335 for (j = 0; j < num_columns(); ++j)
340 <<
",m="<<((int)this->p_memory)<<
","
341 <<
"r'="<<((int)((T *) mx_move_pointer_f(this->p_memory,
sizeof(T)*p_row_step, i)))<<
","
342 <<
"r="<<((int)mx_move_pointer(this->p_memory, T, p_row_step, i))<<
","
343 <<
"c="<<((
int)mx_move_pointer(this->p_memory, T, this->p_column_step, j))<<
","
344 <<((
int)(&fast_a_m_gcc(i,j)))
347 << a_no_check(i,j) <<
"\t";
365 EST_error(
"Matrix loading not implemented yet.");
366 return misc_read_error;
372 int rows,
int columns,
373 int free_when_destroyed)
377 p_row_step = columns;
382 int offset,
int num)
const
384 int to = num >= 0 ? offset + num : num_columns();
386 if (!EST_matrix_bounds_check(r, 0, num_rows(), num_columns(), FALSE))
394 for (
int j = offset; j < to; j++)
395 buf[j-offset] = fast_a_m(r, j);
400 int offset,
int num)
const
402 int to = num >= 0 ? offset + num : num_columns();
404 if (!EST_matrix_bounds_check(r, 0, num_rows(), num_columns(), FALSE))
414 for (
int j = offset; j < to; j++)
415 buf[j - offset] = fast_a_m(r, j);
421 int offset,
int num)
const
426 int to = num >= 0 ? offset + num : num_rows();
428 if (!EST_matrix_bounds_check(0, c, num_rows(), num_columns(), FALSE))
436 for (
int i = offset; i < to; i++)
437 buf[i-offset] = fast_a_m(i, c);
443 int offset,
int num)
const
448 int to = num >= 0 ? offset + num : num_rows();
450 if (!EST_matrix_bounds_check(0, c, num_rows(), num_columns(), FALSE))
452 if( num_columns()>0 )
460 for (
int i = offset; i < to; i++)
461 buf[i-offset] = fast_a_m(i, c);
468 int to = num>=0?offset+num:num_columns();
470 if (!EST_matrix_bounds_check(r, 0, num_rows(), num_columns(), TRUE))
473 for(
int j=offset; j<to; j++)
474 fast_a_m(r, j) = buf[j-offset];
480 int to = num>=0?offset+num:num_rows();
482 if (!EST_matrix_bounds_check(0, c, num_rows(), num_columns(), TRUE))
485 for(
int i=offset; i<to; i++)
486 fast_a_m(i, c) = buf[i-offset];
494 int to = num>=0?offset+num:num_columns();
496 if (!EST_matrix_bounds_check(r, 0, num_rows(), num_columns(), TRUE))
507 for(
int j=offset; j<to; j++)
508 fast_a_m(r, j) = from.
fast_a_m(from_r, (j-offset)+from_offset);
516 int to = num>=0?offset+num:num_rows();
518 if (!EST_matrix_bounds_check(0, c, num_rows(), num_columns(), TRUE))
529 for(
int i=offset; i<to; i++)
530 fast_a_m(i, c) = from.
fast_a_m((i-offset)+from_offset, from_c);
537 len = num_columns()-start_c;
539 if (!EST_matrix_bounds_check(r, 1, start_c, len, num_rows(), num_columns(), 0))
542 if (rv.
p_memory != NULL && ! rv.p_sub_matrix)
545 rv.p_sub_matrix = TRUE;
547 rv.
p_offset = this->p_offset + start_c*this->p_column_step + r*p_row_step;
552 rv.p_column_step=this->p_column_step;
559 len = num_rows()-start_r;
561 if (!EST_matrix_bounds_check(start_r, len, c, 1,num_rows(), num_columns(), 0))
564 if (cv.
p_memory != NULL && ! cv.p_sub_matrix)
567 cv.p_sub_matrix = TRUE;
569 cv.
p_offset = this->p_offset + c*this->p_column_step + start_r*p_row_step;
574 cv.p_column_step=p_row_step;
579 int r,
int len_r,
int c,
int len_c)
582 len_r = num_rows()-r;
584 len_c = num_columns()-c;
586 if (!EST_matrix_bounds_check(r, len_r, c, len_c, num_rows(), num_columns(), 0))
589 if (sm.
p_memory != NULL && ! sm.p_sub_matrix)
592 sm.p_sub_matrix = TRUE;
593 sm.
p_offset = this->p_offset + c*this->p_column_step + r*p_row_step;
596 sm.p_column_step=this->p_column_step;