GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DiagArray2.cc
Go to the documentation of this file.
1 // Template array classes
2 /*
3 
4 Copyright (C) 1996-2017 John W. Eaton
5 Copyright (C) 2010 VZLU Prague
6 
7 This file is part of Octave.
8 
9 Octave is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13 
14 Octave is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Octave; see the file COPYING. If not, see
21 <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 // This file should not include config.h. It is only included in other
26 // C++ source files that should have included config.h before including
27 // this file.
28 
29 #include <cassert>
30 
31 #include <iostream>
32 
33 #include <algorithm>
34 
35 #include "DiagArray2.h"
36 
37 #include "lo-error.h"
38 
39 template <typename T>
42  : Array<T> (a.as_column ()), d1 (r), d2 (c)
43 {
44  octave_idx_type rcmin = std::min (r, c);
45  if (rcmin != a.numel ())
46  Array<T>::resize (dim_vector (rcmin, 1));
47 }
48 
49 template <typename T>
52 {
53  return extract_diag (k);
54 }
55 
56 template <typename T>
59 {
60  Array<T> d;
61 
62  if (k == 0)
63  // The main diagonal is shallow-copied.
64  d = *this;
65  else if (k > 0 && k < cols ())
66  d = Array<T> (dim_vector (std::min (cols () - k, rows ()), 1), T ());
67  else if (k < 0 && -k < rows ())
68  d = Array<T> (dim_vector (std::min (rows () + k, cols ()), 1), T ());
69  else // Matlab returns [] 0x1 for out-of-range diagonal
70  d.resize (dim_vector (0, 1));
71 
72  return d;
73 }
74 
75 template <typename T>
78 {
79  return DiagArray2<T> (*this, d2, d1);
80 }
81 
82 template <typename T>
84 DiagArray2<T>::hermitian (T (* fcn) (const T&)) const
85 {
86  return DiagArray2<T> (Array<T>::template map<T> (fcn), d2, d1);
87 }
88 
89 // A two-dimensional array with diagonal elements only.
90 
91 template <typename T>
92 T&
94 {
95  static T zero (0);
96  return (r == c) ? Array<T>::elem (r) : zero;
97 }
98 
99 template <typename T>
100 T&
102 {
103  static T zero (0);
104  return check_idx (r, c) ? elem (r, c) : zero;
105 }
106 
107 template <typename T>
108 void
110  const T& rfv)
111 {
112  if (r < 0 || c < 0)
113  (*current_liboctave_error_handler) ("can't resize to negative dimensions");
114 
115  if (r != dim1 () || c != dim2 ())
116  {
117  Array<T>::resize (dim_vector (std::min (r, c), 1), rfv);
118  d1 = r; d2 = c;
119  }
120 }
121 
122 template <typename T>
123 Array<T>
125 {
126  Array<T> result (dims (), T (0));
127 
128  for (octave_idx_type i = 0, len = length (); i < len; i++)
129  result.xelem (i, i) = dgelem (i);
130 
131  return result;
132 }
133 
134 template <typename T>
135 bool
137 {
138  bool ok = true;
139 
140  if (r < 0 || r >= dim1 ())
141  octave::err_index_out_of_range (2, 1, r+1, dim1 (), dims ());
142 
143  if (c < 0 || c >= dim2 ())
144  octave::err_index_out_of_range (2, 2, c+1, dim2 (), dims ());
145 
146  return ok;
147 }
void resize(octave_idx_type n, octave_idx_type m, const T &rfv)
Definition: DiagArray2.cc:109
octave_idx_type cols(void) const
Definition: Sparse.h:272
octave_idx_type rows(void) const
Definition: Sparse.h:271
T elem(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.h:114
Array< T > diag(octave_idx_type k=0) const
Definition: DiagArray2.cc:51
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
for large enough k
Definition: lu.cc:606
T checkelem(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.h:127
void err_index_out_of_range(int nd, int dim, octave_idx_type idx, octave_idx_type ext)
is greater than zero
Definition: load-path.cc:2339
bool check_idx(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.cc:136
Array< T > extract_diag(octave_idx_type k=0) const
Definition: DiagArray2.cc:58
octave_idx_type dim1(void) const
Definition: Sparse.h:268
DiagArray2(void)
Definition: DiagArray2.h:50
T & elem(octave_idx_type n)
Definition: Array.h:482
DiagArray2< T > hermitian(T(*fcn)(const T &)=0) const
Definition: DiagArray2.cc:84
octave_function * fcn
Definition: ov-class.cc:1743
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:398
static int elem
Definition: __contourc__.cc:50
void resize(const dim_vector &dv, const T &rfv)
Definition: Array.cc:1028
the sparsity preserving column transformation such that that defines the pivoting threshold can be given in which case it defines the c
Definition: lu.cc:138
the exceeded dimensions are set to if fewer subscripts than dimensions are the exceeding dimensions are merged into the final requested dimension For consider the following dims
Definition: sub2ind.cc:255
With real return the complex result
Definition: data.cc:3375
Array< T > array_value(void) const
Definition: DiagArray2.cc:124
T & xelem(octave_idx_type n)
Definition: Array.h:455
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:126
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
DiagArray2< T > transpose(void) const
Definition: DiagArray2.cc:77
octave_idx_type dim2(void) const
Definition: Sparse.h:269
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:205