GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
DiagArray2.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 John W. Eaton
4 Copyright (C) 2010 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software: you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 // This file should not include config.h. It is only included in other
25 // C++ source files that should have included config.h before including
26 // this file.
27 
28 #include <cassert>
29 
30 #include <iostream>
31 
32 #include <algorithm>
33 
34 #include "DiagArray2.h"
35 
36 #include "lo-error.h"
37 
38 template <typename T>
41  : Array<T> (a.as_column ()), d1 (r), d2 (c)
42 {
43  octave_idx_type rcmin = std::min (r, c);
44  if (rcmin != a.numel ())
45  Array<T>::resize (dim_vector (rcmin, 1));
46 }
47 
48 template <typename T>
51 {
52  Array<T> d;
53 
54  if (k == 0)
55  // The main diagonal is shallow-copied.
56  d = *this;
57  else if (k > 0 && k < cols ())
58  d = Array<T> (dim_vector (std::min (cols () - k, rows ()), 1), T ());
59  else if (k < 0 && -k < rows ())
60  d = Array<T> (dim_vector (std::min (rows () + k, cols ()), 1), T ());
61  else // Matlab returns [] 0x1 for out-of-range diagonal
62  d.resize (dim_vector (0, 1));
63 
64  return d;
65 }
66 
67 template <typename T>
70 {
71  return DiagArray2<T> (*this, d2, d1);
72 }
73 
74 template <typename T>
76 DiagArray2<T>::hermitian (T (* fcn) (const T&)) const
77 {
78  return DiagArray2<T> (Array<T>::template map<T> (fcn), d2, d1);
79 }
80 
81 // A two-dimensional array with diagonal elements only.
82 
83 template <typename T>
84 T&
86 {
87  static T zero (0);
88  return (r == c) ? Array<T>::elem (r) : zero;
89 }
90 
91 template <typename T>
92 T&
94 {
95  static T zero (0);
96  return check_idx (r, c) ? elem (r, c) : zero;
97 }
98 
99 template <typename T>
100 void
102  const T& rfv)
103 {
104  if (r < 0 || c < 0)
105  (*current_liboctave_error_handler) ("can't resize to negative dimensions");
106 
107  if (r != dim1 () || c != dim2 ())
108  {
109  Array<T>::resize (dim_vector (std::min (r, c), 1), rfv);
110  d1 = r; d2 = c;
111  }
112 }
113 
114 template <typename T>
115 Array<T>
117 {
118  Array<T> result (dims (), T (0));
119 
120  for (octave_idx_type i = 0, len = length (); i < len; i++)
121  result.xelem (i, i) = dgelem (i);
122 
123  return result;
124 }
125 
126 template <typename T>
127 bool
129 {
130  bool ok = true;
131 
132  if (r < 0 || r >= dim1 ())
133  octave::err_index_out_of_range (2, 1, r+1, dim1 (), dims ());
134 
135  if (c < 0 || c >= dim2 ())
136  octave::err_index_out_of_range (2, 2, c+1, dim2 (), dims ());
137 
138  return ok;
139 }
void resize(octave_idx_type n, octave_idx_type m, const T &rfv)
Definition: DiagArray2.cc:101
T checkelem(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.h:128
OCTAVE_EXPORT octave_value_list or N dimensional array whose elements are all equal to the IEEE symbol zero divided by zero($0/0$)
for large enough k
Definition: lu.cc:617
void err_index_out_of_range(int nd, int dim, octave_idx_type idx, octave_idx_type ext)
DiagArray2< T > transpose(void) const
Definition: DiagArray2.cc:69
DiagArray2(void)
Definition: DiagArray2.h:49
T & elem(octave_idx_type n)
Definition: Array.h:488
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
T elem(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.h:115
octave_function * fcn
Definition: ov-class.cc:1754
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 const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
DiagArray2< T > hermitian(T(*fcn)(const T &)=nullptr) const
Definition: DiagArray2.cc:76
bool check_idx(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.cc:128
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:400
static int elem
Definition: __contourc__.cc:47
void resize(const dim_vector &dv, const T &rfv)
Resizing (with fill).
Definition: Array.cc:1010
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:3260
octave_idx_type cols(void) const
Definition: Sparse.h:259
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:125
octave_idx_type dim1(void) const
Definition: Sparse.h:255
Array< T > extract_diag(octave_idx_type k=0) const
Definition: DiagArray2.cc:50
for i
Definition: data.cc:5264
Array< T > array_value(void) const
Definition: DiagArray2.cc:116
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
octave_idx_type dim2(void) const
Definition: Sparse.h:256
octave_idx_type rows(void) const
Definition: Sparse.h:258
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:204