GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
DiagArray2.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26// This file should not include config.h. It is only included in other
27// C++ source files that should have included config.h before including
28// this file.
29
30#include <algorithm>
31
32#include "DiagArray2.h"
33
34#include "lo-error.h"
35
36template <typename T>
39 : Array<T> (a.as_column ()), m_d1 (r), m_d2 (c)
40{
41 octave_idx_type rcmin = std::min (r, c);
42 if (rcmin != a.numel ())
43 Array<T>::resize (dim_vector (rcmin, 1));
44}
45
46template <typename T>
49{
50 Array<T> d;
51
52 if (k == 0)
53 // The main diagonal is shallow-copied.
54 d = *this;
55 else if (k > 0 && k < cols ())
56 d = Array<T> (dim_vector (std::min (cols () - k, rows ()), 1), T ());
57 else if (k < 0 && -k < rows ())
58 d = Array<T> (dim_vector (std::min (rows () + k, cols ()), 1), T ());
59 else // Matlab returns [] 0x1 for out-of-range diagonal
60 d.resize (dim_vector (0, 1));
61
62 return d;
63}
64
65template <typename T>
68{
69 return DiagArray2<T> (*this, m_d2, m_d1);
70}
71
72template <typename T>
74DiagArray2<T>::hermitian (T (* fcn) (const T&)) const
75{
76 return DiagArray2<T> (Array<T>::template map<T> (fcn), m_d2, m_d1);
77}
78
79// A two-dimensional array with diagonal elements only.
80
81template <typename T>
82T&
84{
85 static T zero (0);
86 return (r == c) ? Array<T>::elem (r) : zero;
87}
88
89template <typename T>
90T&
92{
93 static T zero (0);
94 return check_idx (r, c) ? elem (r, c) : zero;
95}
96
97template <typename T>
98void
100 const T& rfv)
101{
102 if (r < 0 || c < 0)
103 (*current_liboctave_error_handler) ("can't resize to negative dimensions");
105 if (r != dim1 () || c != dim2 ())
106 {
107 Array<T>::resize (dim_vector (std::min (r, c), 1), rfv);
108 m_d1 = r; m_d2 = c;
109 }
110}
111
112template <typename T>
115{
116 Array<T> result (dims (), T (0));
117
118 for (octave_idx_type i = 0, len = length (); i < len; i++)
119 result.xelem (i, i) = dgelem (i);
120
121 return result;
122}
123
124template <typename T>
125bool
127{
128 bool ok = true;
129
130 if (r < 0 || r >= dim1 ())
131 octave::err_index_out_of_range (2, 1, r+1, dim1 (), dims ());
132
133 if (c < 0 || c >= dim2 ())
134 octave::err_index_out_of_range (2, 2, c+1, dim2 (), dims ());
136 return ok;
137}
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
T & xelem(octave_idx_type n)
Size of the specified dimension.
Definition Array.h:525
T & elem(octave_idx_type n)
Size of the specified dimension.
Definition Array.h:563
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
octave_idx_type numel() const
Number of elements in the array.
Definition Array.h:418
void resize(octave_idx_type n, octave_idx_type m, const T &rfv)
Definition DiagArray2.cc:99
Array< T > array_value() const
Array< T > extract_diag(octave_idx_type k=0) const
Definition DiagArray2.cc:48
T elem(octave_idx_type r, octave_idx_type c) const
Definition DiagArray2.h:114
T checkelem(octave_idx_type r, octave_idx_type c) const
Definition DiagArray2.h:127
DiagArray2< T > hermitian(T(*fcn)(const T &)=nullptr) const
Definition DiagArray2.cc:74
DiagArray2< T > transpose() const
Definition DiagArray2.cc:67
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T len
Definition xerbla.cc:61