ngsolve 6.2.2506.post74.dev0__cp314-cp314-macosx_10_15_universal2.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of ngsolve might be problematic. Click here for more details.
- netgen/include/analytic_integrals.hpp +10 -0
- netgen/include/arnoldi.hpp +55 -0
- netgen/include/bandmatrix.hpp +334 -0
- netgen/include/basematrix.hpp +957 -0
- netgen/include/basevector.hpp +1268 -0
- netgen/include/bdbequations.hpp +2805 -0
- netgen/include/bdbintegrator.hpp +1660 -0
- netgen/include/bem_diffops.hpp +475 -0
- netgen/include/bessel.hpp +1064 -0
- netgen/include/bilinearform.hpp +963 -0
- netgen/include/bla.hpp +29 -0
- netgen/include/blockalloc.hpp +95 -0
- netgen/include/blockjacobi.hpp +328 -0
- netgen/include/bspline.hpp +116 -0
- netgen/include/calcinverse.hpp +141 -0
- netgen/include/cg.hpp +368 -0
- netgen/include/chebyshev.hpp +44 -0
- netgen/include/cholesky.hpp +720 -0
- netgen/include/clapack.h +7254 -0
- netgen/include/code_generation.hpp +296 -0
- netgen/include/coefficient.hpp +2033 -0
- netgen/include/coefficient_impl.hpp +19 -0
- netgen/include/coefficient_stdmath.hpp +167 -0
- netgen/include/commutingAMG.hpp +106 -0
- netgen/include/comp.hpp +79 -0
- netgen/include/compatibility.hpp +41 -0
- netgen/include/complex_wrapper.hpp +73 -0
- netgen/include/compressedfespace.hpp +110 -0
- netgen/include/contact.hpp +235 -0
- netgen/include/diagonalmatrix.hpp +154 -0
- netgen/include/differentialoperator.hpp +276 -0
- netgen/include/diffop.hpp +1286 -0
- netgen/include/diffop_impl.hpp +328 -0
- netgen/include/diffopwithfactor.hpp +123 -0
- netgen/include/discontinuous.hpp +84 -0
- netgen/include/dump.hpp +949 -0
- netgen/include/ectypes.hpp +121 -0
- netgen/include/eigen.hpp +60 -0
- netgen/include/eigensystem.hpp +18 -0
- netgen/include/elasticity_equations.hpp +595 -0
- netgen/include/elementbyelement.hpp +195 -0
- netgen/include/elementtopology.hpp +1760 -0
- netgen/include/elementtransformation.hpp +339 -0
- netgen/include/evalfunc.hpp +405 -0
- netgen/include/expr.hpp +1686 -0
- netgen/include/facetfe.hpp +175 -0
- netgen/include/facetfespace.hpp +180 -0
- netgen/include/facethofe.hpp +111 -0
- netgen/include/facetsurffespace.hpp +112 -0
- netgen/include/fe_interfaces.hpp +32 -0
- netgen/include/fem.hpp +87 -0
- netgen/include/fesconvert.hpp +14 -0
- netgen/include/fespace.hpp +1449 -0
- netgen/include/finiteelement.hpp +286 -0
- netgen/include/globalinterfacespace.hpp +77 -0
- netgen/include/globalspace.hpp +115 -0
- netgen/include/gridfunction.hpp +525 -0
- netgen/include/h1amg.hpp +124 -0
- netgen/include/h1hofe.hpp +188 -0
- netgen/include/h1hofe_impl.hpp +1262 -0
- netgen/include/h1hofefo.hpp +148 -0
- netgen/include/h1hofefo_impl.hpp +185 -0
- netgen/include/h1hofespace.hpp +167 -0
- netgen/include/h1lofe.hpp +1240 -0
- netgen/include/h1lumping.hpp +41 -0
- netgen/include/hcurl_equations.hpp +1381 -0
- netgen/include/hcurlcurlfe.hpp +2241 -0
- netgen/include/hcurlcurlfespace.hpp +78 -0
- netgen/include/hcurlfe.hpp +259 -0
- netgen/include/hcurlfe_utils.hpp +107 -0
- netgen/include/hcurlhdiv_dshape.hpp +857 -0
- netgen/include/hcurlhdivfes.hpp +308 -0
- netgen/include/hcurlhofe.hpp +175 -0
- netgen/include/hcurlhofe_impl.hpp +1871 -0
- netgen/include/hcurlhofespace.hpp +193 -0
- netgen/include/hcurllofe.hpp +1146 -0
- netgen/include/hdiv_equations.hpp +880 -0
- netgen/include/hdivdivfe.hpp +2923 -0
- netgen/include/hdivdivsurfacespace.hpp +76 -0
- netgen/include/hdivfe.hpp +206 -0
- netgen/include/hdivfe_utils.hpp +717 -0
- netgen/include/hdivfes.hpp +75 -0
- netgen/include/hdivhofe.hpp +447 -0
- netgen/include/hdivhofe_impl.hpp +1107 -0
- netgen/include/hdivhofefo.hpp +229 -0
- netgen/include/hdivhofespace.hpp +177 -0
- netgen/include/hdivhosurfacefespace.hpp +106 -0
- netgen/include/hdivlofe.hpp +773 -0
- netgen/include/hidden.hpp +74 -0
- netgen/include/householder.hpp +181 -0
- netgen/include/hypre_ams_precond.hpp +123 -0
- netgen/include/hypre_precond.hpp +73 -0
- netgen/include/integrator.hpp +2012 -0
- netgen/include/integratorcf.hpp +253 -0
- netgen/include/interpolate.hpp +49 -0
- netgen/include/intrule.hpp +2542 -0
- netgen/include/intrules_SauterSchwab.hpp +25 -0
- netgen/include/irspace.hpp +49 -0
- netgen/include/jacobi.hpp +153 -0
- netgen/include/kernels.hpp +762 -0
- netgen/include/l2hofe.hpp +194 -0
- netgen/include/l2hofe_impl.hpp +564 -0
- netgen/include/l2hofefo.hpp +542 -0
- netgen/include/l2hofespace.hpp +344 -0
- netgen/include/la.hpp +38 -0
- netgen/include/linearform.hpp +266 -0
- netgen/include/matrix.hpp +2140 -0
- netgen/include/memusage.hpp +41 -0
- netgen/include/meshaccess.hpp +1359 -0
- netgen/include/mgpre.hpp +204 -0
- netgen/include/mp_coefficient.hpp +145 -0
- netgen/include/mptools.hpp +2281 -0
- netgen/include/multigrid.hpp +42 -0
- netgen/include/multivector.hpp +447 -0
- netgen/include/mumpsinverse.hpp +187 -0
- netgen/include/mycomplex.hpp +361 -0
- netgen/include/ng_lapack.hpp +1661 -0
- netgen/include/ngblas.hpp +1232 -0
- netgen/include/ngs_defines.hpp +30 -0
- netgen/include/ngs_stdcpp_include.hpp +106 -0
- netgen/include/ngs_utils.hpp +121 -0
- netgen/include/ngsobject.hpp +1019 -0
- netgen/include/ngsstream.hpp +113 -0
- netgen/include/ngstd.hpp +72 -0
- netgen/include/nodalhofe.hpp +96 -0
- netgen/include/nodalhofe_impl.hpp +141 -0
- netgen/include/normalfacetfe.hpp +223 -0
- netgen/include/normalfacetfespace.hpp +98 -0
- netgen/include/normalfacetsurfacefespace.hpp +84 -0
- netgen/include/order.hpp +251 -0
- netgen/include/parallel_matrices.hpp +222 -0
- netgen/include/paralleldofs.hpp +340 -0
- netgen/include/parallelngs.hpp +23 -0
- netgen/include/parallelvector.hpp +269 -0
- netgen/include/pardisoinverse.hpp +200 -0
- netgen/include/periodic.hpp +129 -0
- netgen/include/plateaufespace.hpp +25 -0
- netgen/include/pml.hpp +275 -0
- netgen/include/pmltrafo.hpp +631 -0
- netgen/include/postproc.hpp +142 -0
- netgen/include/potentialtools.hpp +22 -0
- netgen/include/precomp.hpp +60 -0
- netgen/include/preconditioner.hpp +602 -0
- netgen/include/prolongation.hpp +377 -0
- netgen/include/python_comp.hpp +107 -0
- netgen/include/python_fem.hpp +89 -0
- netgen/include/python_linalg.hpp +58 -0
- netgen/include/python_ngstd.hpp +386 -0
- netgen/include/recursive_pol.hpp +4896 -0
- netgen/include/recursive_pol_tet.hpp +395 -0
- netgen/include/recursive_pol_trig.hpp +492 -0
- netgen/include/reorderedfespace.hpp +81 -0
- netgen/include/sample_sort.hpp +105 -0
- netgen/include/scalarfe.hpp +335 -0
- netgen/include/shapefunction_utils.hpp +113 -0
- netgen/include/simd_complex.hpp +329 -0
- netgen/include/smoother.hpp +253 -0
- netgen/include/solve.hpp +89 -0
- netgen/include/sparsecholesky.hpp +313 -0
- netgen/include/sparsematrix.hpp +1038 -0
- netgen/include/sparsematrix_dyn.hpp +90 -0
- netgen/include/sparsematrix_impl.hpp +1013 -0
- netgen/include/special_matrix.hpp +463 -0
- netgen/include/specialelement.hpp +125 -0
- netgen/include/statushandler.hpp +33 -0
- netgen/include/stringops.hpp +12 -0
- netgen/include/superluinverse.hpp +136 -0
- netgen/include/symbolicintegrator.hpp +850 -0
- netgen/include/symmetricmatrix.hpp +144 -0
- netgen/include/tangentialfacetfe.hpp +224 -0
- netgen/include/tangentialfacetfespace.hpp +91 -0
- netgen/include/tensor.hpp +522 -0
- netgen/include/tensorcoefficient.hpp +446 -0
- netgen/include/tensorproductintegrator.hpp +113 -0
- netgen/include/thcurlfe.hpp +128 -0
- netgen/include/thcurlfe_impl.hpp +380 -0
- netgen/include/thdivfe.hpp +80 -0
- netgen/include/thdivfe_impl.hpp +492 -0
- netgen/include/tpdiffop.hpp +461 -0
- netgen/include/tpfes.hpp +133 -0
- netgen/include/tpintrule.hpp +224 -0
- netgen/include/triangular.hpp +465 -0
- netgen/include/tscalarfe.hpp +245 -0
- netgen/include/tscalarfe_impl.hpp +1029 -0
- netgen/include/umfpackinverse.hpp +148 -0
- netgen/include/vector.hpp +1273 -0
- netgen/include/voxelcoefficientfunction.hpp +41 -0
- netgen/include/vtkoutput.hpp +198 -0
- netgen/include/vvector.hpp +208 -0
- netgen/include/webgui.hpp +92 -0
- netgen/libngbla.dylib +0 -0
- netgen/libngcomp.dylib +0 -0
- netgen/libngfem.dylib +0 -0
- netgen/libngla.dylib +0 -0
- netgen/libngsbem.dylib +0 -0
- netgen/libngsolve.dylib +0 -0
- netgen/libngstd.dylib +0 -0
- ngsolve/TensorProductTools.py +210 -0
- ngsolve/__console.py +94 -0
- ngsolve/__expr.py +181 -0
- ngsolve/__init__.py +148 -0
- ngsolve/__init__.pyi +233 -0
- ngsolve/_scikit_build_core_dependencies.py +30 -0
- ngsolve/bla.pyi +1153 -0
- ngsolve/bvp.py +78 -0
- ngsolve/bvp.pyi +32 -0
- ngsolve/cmake/NGSolveConfig.cmake +102 -0
- ngsolve/cmake/ngsolve-targets-release.cmake +79 -0
- ngsolve/cmake/ngsolve-targets.cmake +163 -0
- ngsolve/comp/__init__.pyi +5449 -0
- ngsolve/comp/pml.pyi +89 -0
- ngsolve/config/__init__.py +1 -0
- ngsolve/config/__init__.pyi +43 -0
- ngsolve/config/__main__.py +4 -0
- ngsolve/config/config.py +60 -0
- ngsolve/config/config.pyi +45 -0
- ngsolve/demos/TensorProduct/__init__.py +0 -0
- ngsolve/demos/TensorProduct/tp_dg_1d_1d.py +80 -0
- ngsolve/demos/TensorProduct/tp_dg_1d_2d.py +73 -0
- ngsolve/demos/TensorProduct/tp_dg_2d_1d.py +72 -0
- ngsolve/demos/TensorProduct/tp_dg_2d_2d.py +66 -0
- ngsolve/demos/__init__.py +0 -0
- ngsolve/demos/howto/__init__.py +0 -0
- ngsolve/demos/howto/hhj.py +44 -0
- ngsolve/demos/howto/hybrid_dg.py +53 -0
- ngsolve/demos/howto/mixed.py +30 -0
- ngsolve/demos/howto/nonlin.py +29 -0
- ngsolve/demos/howto/pickling.py +26 -0
- ngsolve/demos/howto/pml.py +31 -0
- ngsolve/demos/howto/taskmanager.py +20 -0
- ngsolve/demos/howto/tdnns.py +47 -0
- ngsolve/demos/howto/timeDG-skeleton.py +45 -0
- ngsolve/demos/howto/timeDG.py +38 -0
- ngsolve/demos/howto/timeDGlap.py +42 -0
- ngsolve/demos/howto/timeDGwave.py +61 -0
- ngsolve/demos/intro/__init__.py +0 -0
- ngsolve/demos/intro/adaptive.py +123 -0
- ngsolve/demos/intro/cmagnet.py +59 -0
- ngsolve/demos/intro/elasticity.py +76 -0
- ngsolve/demos/intro/navierstokes.py +74 -0
- ngsolve/demos/intro/poisson.ipynb +170 -0
- ngsolve/demos/intro/poisson.py +41 -0
- ngsolve/demos/mpi/__init__.py +0 -0
- ngsolve/demos/mpi/mpi_cmagnet.py +87 -0
- ngsolve/demos/mpi/mpi_navierstokes.py +117 -0
- ngsolve/demos/mpi/mpi_poisson.py +89 -0
- ngsolve/demos/mpi/mpi_timeDG.py +82 -0
- ngsolve/directsolvers.py +26 -0
- ngsolve/directsolvers.pyi +15 -0
- ngsolve/eigenvalues.py +364 -0
- ngsolve/eigenvalues.pyi +30 -0
- ngsolve/fem.pyi +1647 -0
- ngsolve/internal.py +89 -0
- ngsolve/krylovspace.py +1013 -0
- ngsolve/krylovspace.pyi +298 -0
- ngsolve/la.pyi +1230 -0
- ngsolve/meshes.py +748 -0
- ngsolve/ngs2petsc.py +310 -0
- ngsolve/ngscxx.py +42 -0
- ngsolve/ngslib.so +0 -0
- ngsolve/ngstd.pyi +59 -0
- ngsolve/nonlinearsolvers.py +203 -0
- ngsolve/nonlinearsolvers.pyi +95 -0
- ngsolve/preconditioners.py +11 -0
- ngsolve/preconditioners.pyi +7 -0
- ngsolve/solve.pyi +109 -0
- ngsolve/solve_implementation.py +168 -0
- ngsolve/solve_implementation.pyi +42 -0
- ngsolve/solvers.py +7 -0
- ngsolve/solvers.pyi +14 -0
- ngsolve/timestepping.py +185 -0
- ngsolve/timestepping.pyi +28 -0
- ngsolve/timing.py +108 -0
- ngsolve/timing.pyi +54 -0
- ngsolve/utils.py +167 -0
- ngsolve/utils.pyi +273 -0
- ngsolve/webgui.py +670 -0
- ngsolve-6.2.2506.post74.dev0.data/data/Netgen.icns +0 -0
- ngsolve-6.2.2506.post74.dev0.data/data/bin/ngscxx +17 -0
- ngsolve-6.2.2506.post74.dev0.data/data/bin/ngsld +13 -0
- ngsolve-6.2.2506.post74.dev0.data/data/bin/ngsolve.tcl +648 -0
- ngsolve-6.2.2506.post74.dev0.data/data/bin/ngspy +2 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/beam.geo +17 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/beam.vol +240 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/chip.in2d +41 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/chip.vol +614 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/coil.geo +12 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/coil.vol +2560 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/coilshield.geo +24 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/coilshield.vol +3179 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/cube.geo +19 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/cube.vol +1832 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/d10_DGdoubleglazing.pde +50 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/d11_chip_nitsche.pde +40 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/d1_square.pde +43 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/d2_chip.pde +35 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/d3_helmholtz.pde +22 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/d4_cube.pde +46 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/d5_beam.pde +74 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/d6_shaft.pde +73 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/d7_coil.pde +50 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/d8_coilshield.pde +49 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/d9_hybridDG.pde +72 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/doubleglazing.in2d +27 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/doubleglazing.vol +737 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/piezo2d40round4.vol.gz +0 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/shaft.geo +73 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/shaft.vol +4291 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/square.in2d +17 -0
- ngsolve-6.2.2506.post74.dev0.data/data/share/ngsolve/square.vol +149 -0
- ngsolve-6.2.2506.post74.dev0.dist-info/METADATA +13 -0
- ngsolve-6.2.2506.post74.dev0.dist-info/RECORD +315 -0
- ngsolve-6.2.2506.post74.dev0.dist-info/WHEEL +5 -0
- ngsolve-6.2.2506.post74.dev0.dist-info/licenses/LICENSE +504 -0
- ngsolve-6.2.2506.post74.dev0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,2140 @@
|
|
|
1
|
+
#ifndef FILE_MATRIX_EXPR
|
|
2
|
+
#define FILE_MATRIX_EXPR
|
|
3
|
+
|
|
4
|
+
/**************************************************************************/
|
|
5
|
+
/* File: matrix.hpp */
|
|
6
|
+
/* Author: Joachim Schoeberl */
|
|
7
|
+
/* Date: 01. Jan. 02 */
|
|
8
|
+
/**************************************************************************/
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
#include "expr.hpp"
|
|
12
|
+
#include "vector.hpp"
|
|
13
|
+
|
|
14
|
+
namespace ngbla
|
|
15
|
+
{
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
template <typename T = double, ORDERING ORD = RowMajor>
|
|
19
|
+
using SliceMatrix = MatrixView<T,ORD,size_t, size_t, size_t>;
|
|
20
|
+
|
|
21
|
+
template <typename T, typename TELEM=typename std::remove_reference_t<T>::TELEM>
|
|
22
|
+
constexpr bool IsConvertibleToSliceMatrix ()
|
|
23
|
+
{
|
|
24
|
+
/*
|
|
25
|
+
return is_convertible_v<T,SliceMatrix<TELEM, RowMajor>> ||
|
|
26
|
+
is_convertible_v<T,SliceMatrix<TELEM, ColMajor>>;
|
|
27
|
+
*/
|
|
28
|
+
return is_constructible_v<SliceMatrix<TELEM, RowMajor>,T> ||
|
|
29
|
+
is_constructible_v<SliceMatrix<TELEM, ColMajor>,T>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
template <typename T = double, ORDERING ORD = RowMajor>
|
|
33
|
+
using BareSliceMatrix = MatrixView<T,ORD,undefined_size, undefined_size, size_t>;
|
|
34
|
+
|
|
35
|
+
template <typename T, typename TELEM=typename std::remove_reference_t<T>::TELEM>
|
|
36
|
+
constexpr bool IsConvertibleToBareSliceMatrix ()
|
|
37
|
+
{
|
|
38
|
+
// return is_convertible_v<T,BareSliceMatrix<TELEM, RowMajor>> ||
|
|
39
|
+
// is_convertible_v<T,BareSliceMatrix<TELEM, ColMajor>>;
|
|
40
|
+
return is_constructible_v<BareSliceMatrix<TELEM, RowMajor>,T> ||
|
|
41
|
+
is_constructible_v<BareSliceMatrix<TELEM, ColMajor>,T>;
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
template <int H, int W, typename T> class Mat;
|
|
49
|
+
template <typename T> class DoubleSliceMatrix;
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
#ifdef OLDFLATMATRIX
|
|
53
|
+
/**
|
|
54
|
+
A simple matrix.
|
|
55
|
+
Has height, width and data-pointer.
|
|
56
|
+
No memory allocation/deallocation. User must provide memory.
|
|
57
|
+
*/
|
|
58
|
+
template <typename T, ORDERING ORD>
|
|
59
|
+
class FlatMatrix : public MatExpr<FlatMatrix<T,ORD> >
|
|
60
|
+
{
|
|
61
|
+
protected:
|
|
62
|
+
typedef MatExpr<FlatMatrix<T,ORD>> BASE;
|
|
63
|
+
/// the height
|
|
64
|
+
size_t h;
|
|
65
|
+
/// the width
|
|
66
|
+
size_t w;
|
|
67
|
+
/// the data
|
|
68
|
+
T * __restrict data;
|
|
69
|
+
public:
|
|
70
|
+
|
|
71
|
+
/// element type
|
|
72
|
+
typedef T TELEM;
|
|
73
|
+
/// scalar type of elements (double or Complex)
|
|
74
|
+
typedef typename mat_traits<T>::TSCAL TSCAL;
|
|
75
|
+
static constexpr bool IsLinear() { return ORD==RowMajor; }
|
|
76
|
+
/// nothing done in default constructor
|
|
77
|
+
INLINE FlatMatrix () = default;
|
|
78
|
+
|
|
79
|
+
/// set height, width, and mem
|
|
80
|
+
INLINE FlatMatrix (size_t ah, size_t aw, T * adata)
|
|
81
|
+
: h(ah), w(aw), data(adata) { ; }
|
|
82
|
+
|
|
83
|
+
/// set height = width, and mem
|
|
84
|
+
INLINE FlatMatrix (size_t ah, T * adata)
|
|
85
|
+
: h(ah), w(ah), data(adata) { ; }
|
|
86
|
+
|
|
87
|
+
/// allocates at local heap
|
|
88
|
+
INLINE FlatMatrix (size_t ah, size_t aw, LocalHeap & lh)
|
|
89
|
+
: h(ah), w(aw), data (lh.Alloc<T>(ah*aw)) { ; }
|
|
90
|
+
|
|
91
|
+
/// allocates at local heap
|
|
92
|
+
INLINE FlatMatrix (size_t ah, LocalHeap & lh)
|
|
93
|
+
: h(ah), w(ah), data(lh.Alloc<T>(ah*ah)) { ; }
|
|
94
|
+
|
|
95
|
+
/// copy constructor. copies pointers, not contents
|
|
96
|
+
FlatMatrix (const FlatMatrix & m) = default;
|
|
97
|
+
/// allocate and compute
|
|
98
|
+
template<typename TB>
|
|
99
|
+
INLINE FlatMatrix (const LocalHeapExpr<TB> & m2)
|
|
100
|
+
{
|
|
101
|
+
h = m2.A().Height();
|
|
102
|
+
w = m2.A().Width();
|
|
103
|
+
LocalHeap & lh = m2.GetLocalHeap();
|
|
104
|
+
data = lh.Alloc<T> (h*w);
|
|
105
|
+
// CMCPMatExpr<FlatMatrix<T,ORD> >::operator= (m2.A());
|
|
106
|
+
this->operator= (m2.A());
|
|
107
|
+
}
|
|
108
|
+
/*
|
|
109
|
+
/// useful to put FlatMatrix over other matrix
|
|
110
|
+
template <typename T2>
|
|
111
|
+
INLINE explicit FlatMatrix (const MatExpr<T2> & m)
|
|
112
|
+
: h(m.Height()), w(m.Width()),
|
|
113
|
+
data(const_cast<T*>(m.Spec().Data()))
|
|
114
|
+
{
|
|
115
|
+
static_assert(ORD == m.Ordering());
|
|
116
|
+
}
|
|
117
|
+
*/
|
|
118
|
+
|
|
119
|
+
template <int W, enable_if_t<ORD==RowMajor, int> =0>
|
|
120
|
+
INLINE FlatMatrix (MatrixView<T,RowMajor,size_t,IC<W>,IC<W>> m)
|
|
121
|
+
: h(m.Height()), w(m.Width()), data(m.Data()) { }
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
/// useful to put FlatMatrix over other Mat
|
|
125
|
+
template <int H, int W>
|
|
126
|
+
INLINE FlatMatrix (const Mat<H,W,TSCAL> & m)
|
|
127
|
+
: h(H), w(W), data(const_cast<T*>(m.Data()))
|
|
128
|
+
{ ; }
|
|
129
|
+
|
|
130
|
+
// do nothing
|
|
131
|
+
// ~FlatMatrix () throw() { ; }
|
|
132
|
+
|
|
133
|
+
/// set size, and assign mem
|
|
134
|
+
INLINE void AssignMemory (size_t ah, size_t aw, LocalHeap & lh)
|
|
135
|
+
{
|
|
136
|
+
h = ah;
|
|
137
|
+
w = aw;
|
|
138
|
+
data = lh.Alloc<T>(h*w);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/// set size, and assign mem
|
|
142
|
+
INLINE void AssignMemory (size_t ah, size_t aw, T * mem)
|
|
143
|
+
{
|
|
144
|
+
h = ah;
|
|
145
|
+
w = aw;
|
|
146
|
+
data = mem;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
/// assign contents
|
|
151
|
+
template<typename TBxx>
|
|
152
|
+
INLINE FlatMatrix & operator= (const Expr<TBxx> & m)
|
|
153
|
+
{
|
|
154
|
+
BASE::operator= (m);
|
|
155
|
+
return *this;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/// copy contents
|
|
159
|
+
INLINE FlatMatrix & operator= (const FlatMatrix & m)
|
|
160
|
+
{
|
|
161
|
+
// for (size_t i = 0; i < h*w; i++) data[i] = m(i);
|
|
162
|
+
AsVector() = m.AsVector();
|
|
163
|
+
return *this;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/// assign constant
|
|
167
|
+
INLINE FlatMatrix & operator= (TSCAL s)
|
|
168
|
+
{
|
|
169
|
+
// for (auto i : Range(h*w)) data[i] = s;
|
|
170
|
+
AsVector() = s;
|
|
171
|
+
return *this;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
INLINE auto View() const { return FlatMatrix(*this); }
|
|
176
|
+
INLINE auto ViewRW() { return *this; }
|
|
177
|
+
INLINE tuple<size_t, size_t> Shape() const { return { h, w }; }
|
|
178
|
+
static constexpr auto Ordering() { return ORD; }
|
|
179
|
+
|
|
180
|
+
/// copy size and pointers
|
|
181
|
+
INLINE FlatMatrix & Assign (const FlatMatrix & m)
|
|
182
|
+
{
|
|
183
|
+
h = m.h;
|
|
184
|
+
w = m.w;
|
|
185
|
+
data = m.data;
|
|
186
|
+
return *this;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
/// access operator, linear access
|
|
191
|
+
INLINE TELEM & operator() (size_t i) const
|
|
192
|
+
{
|
|
193
|
+
NETGEN_CHECK_RANGE(i, 0, Height()*Width());
|
|
194
|
+
return data[i];
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/// access operator
|
|
198
|
+
template <typename I, typename J>
|
|
199
|
+
INLINE TELEM & operator() (I i, J j) const
|
|
200
|
+
{
|
|
201
|
+
NETGEN_CHECK_RANGE(i, 0, Height());
|
|
202
|
+
NETGEN_CHECK_RANGE(j, 0, Width());
|
|
203
|
+
return data[i*w+j];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/// the height
|
|
207
|
+
INLINE auto Height () const { return h; }
|
|
208
|
+
/// the width
|
|
209
|
+
INLINE auto Width () const { return w; }
|
|
210
|
+
///
|
|
211
|
+
INLINE auto Dist () const { return w; }
|
|
212
|
+
|
|
213
|
+
INLINE T * Data () const { return data; }
|
|
214
|
+
|
|
215
|
+
INLINE FlatVector<T> Row (size_t i) const
|
|
216
|
+
{
|
|
217
|
+
return FlatVector<T> (w, data+i*w);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
INLINE SliceVector<T> Col (size_t i) const
|
|
221
|
+
{
|
|
222
|
+
return SliceVector<T> (h, w, data+i);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
INLINE SliceVector<T> Diag () const
|
|
226
|
+
{
|
|
227
|
+
return SliceVector<T> (h, w+1, &data[0]);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
SliceVector<T> Diag (int offset) const
|
|
231
|
+
{
|
|
232
|
+
int dp = std::max(offset, 0);
|
|
233
|
+
int dm = std::min(offset, 0);
|
|
234
|
+
return SliceVector<T> (std::min(w-dp, h+dm), Dist()+1, data+dp-dm*Dist());
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
using BASE::Rows;
|
|
239
|
+
using BASE::Cols;
|
|
240
|
+
|
|
241
|
+
INLINE FlatMatrix Rows (size_t first, size_t next) const
|
|
242
|
+
{
|
|
243
|
+
return FlatMatrix (next-first, w, data+first*w);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
INLINE FlatMatrix Rows (size_t next) const
|
|
247
|
+
{
|
|
248
|
+
return FlatMatrix (next, w, data);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
INLINE SliceMatrix<T> Cols (size_t first, size_t next) const
|
|
252
|
+
{
|
|
253
|
+
return SliceMatrix<T> (h, next-first, w, data+first);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
INLINE SliceMatrix<T> Cols (size_t next) const
|
|
257
|
+
{
|
|
258
|
+
return SliceMatrix<T> (h, next, w, data);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
INLINE FlatMatrix Rows (IntRange range) const
|
|
262
|
+
{
|
|
263
|
+
return FlatMatrix (range.Next()-range.First(), w, data+range.First()*w);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
INLINE SliceMatrix<T> Cols (IntRange range) const
|
|
267
|
+
{
|
|
268
|
+
return SliceMatrix<T> (h, range.Next()-range.First(), w, data+range.First());
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
BareSliceMatrix<T> RowSlice(size_t first, size_t adist) const
|
|
272
|
+
{
|
|
273
|
+
return BareSliceMatrix<T> (Height()/adist, w, w*adist, data+first*w);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
INLINE auto SplitRows (size_t split) const
|
|
277
|
+
{
|
|
278
|
+
return tuple(Rows(0,split), Rows(split, Height()));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
INLINE auto SplitCols (size_t split) const
|
|
282
|
+
{
|
|
283
|
+
return tuple(Cols(0,split), Cols(split, Width()));
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
auto Bare() const
|
|
288
|
+
{
|
|
289
|
+
return make_BareSliceMatrix(*this);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/*
|
|
293
|
+
INLINE operator SliceMatrix<T> () const
|
|
294
|
+
{
|
|
295
|
+
return SliceMatrix<T> (h, w, w, data);
|
|
296
|
+
}
|
|
297
|
+
*/
|
|
298
|
+
auto AsVector() const
|
|
299
|
+
{
|
|
300
|
+
return FlatVector<T> (h*w, data);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
auto Reshape(size_t h2, size_t w2)
|
|
304
|
+
{
|
|
305
|
+
return FlatMatrix{h2,w2,data};
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
template <typename T>
|
|
314
|
+
class FlatMatrix<T,ColMajor> : public MatExpr<FlatMatrix<T,ColMajor> >
|
|
315
|
+
{
|
|
316
|
+
protected:
|
|
317
|
+
typedef MatExpr<FlatMatrix<T,ColMajor>> BASE;
|
|
318
|
+
size_t h;
|
|
319
|
+
size_t w;
|
|
320
|
+
T * __restrict data;
|
|
321
|
+
public:
|
|
322
|
+
static constexpr bool IsLinear() { return false; }
|
|
323
|
+
/// element type
|
|
324
|
+
typedef T TELEM;
|
|
325
|
+
/// scalar type of elements (double or Complex)
|
|
326
|
+
typedef typename mat_traits<T>::TSCAL TSCAL;
|
|
327
|
+
|
|
328
|
+
/// nothing done in default constructor
|
|
329
|
+
INLINE FlatMatrix () { ; }
|
|
330
|
+
|
|
331
|
+
/// set height, width, and mem
|
|
332
|
+
INLINE FlatMatrix (int ah, int aw, T * adata)
|
|
333
|
+
: h(ah), w(aw), data(adata) { ; }
|
|
334
|
+
|
|
335
|
+
/// set height = width, and mem
|
|
336
|
+
INLINE FlatMatrix (int ah, T * adata)
|
|
337
|
+
: h(ah), w(ah), data(adata) { ; }
|
|
338
|
+
|
|
339
|
+
/// allocates at local heap
|
|
340
|
+
INLINE FlatMatrix (int ah, int aw, LocalHeap & lh)
|
|
341
|
+
: h(ah), w(aw), data (lh.Alloc<T>(ah*aw)) { ; }
|
|
342
|
+
|
|
343
|
+
/// allocates at local heap
|
|
344
|
+
INLINE FlatMatrix (int ah, LocalHeap & lh)
|
|
345
|
+
: h(ah), w(ah), data(lh.Alloc<T>(ah*ah)) { ; }
|
|
346
|
+
|
|
347
|
+
/// copy constructor. copies pointers, not contents
|
|
348
|
+
INLINE FlatMatrix (const FlatMatrix & m)
|
|
349
|
+
: h(m.h), w(m.w) , data(m.data)
|
|
350
|
+
{ ; }
|
|
351
|
+
|
|
352
|
+
/// allocate and compute
|
|
353
|
+
template<typename TB>
|
|
354
|
+
INLINE FlatMatrix (const LocalHeapExpr<TB> & m2)
|
|
355
|
+
{
|
|
356
|
+
h = m2.A().Height();
|
|
357
|
+
w = m2.A().Width();
|
|
358
|
+
LocalHeap & lh = m2.GetLocalHeap();
|
|
359
|
+
data = lh.Alloc<T> (h*w);
|
|
360
|
+
BASE::operator= (m2.A());
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/// set size, and assign mem
|
|
364
|
+
INLINE void AssignMemory (int ah, int aw, LocalHeap & lh)
|
|
365
|
+
{
|
|
366
|
+
h = ah;
|
|
367
|
+
w = aw;
|
|
368
|
+
data = lh.Alloc<T>(h*w);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/// set size, and assign mem
|
|
372
|
+
INLINE void AssignMemory (int ah, int aw, T * mem)
|
|
373
|
+
{
|
|
374
|
+
h = ah;
|
|
375
|
+
w = aw;
|
|
376
|
+
data = mem;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/// assign contents
|
|
380
|
+
template<typename TBxx>
|
|
381
|
+
INLINE FlatMatrix & operator= (const Expr<TBxx> & m)
|
|
382
|
+
{
|
|
383
|
+
BASE::operator= (m);
|
|
384
|
+
return *this;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/// copy contents
|
|
388
|
+
INLINE FlatMatrix & operator= (const FlatMatrix & m)
|
|
389
|
+
{
|
|
390
|
+
for (size_t i = 0; i < size_t(h)*size_t(w); i++) data[i] = m(i);
|
|
391
|
+
return *this;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/// assign constant
|
|
395
|
+
INLINE FlatMatrix & operator= (TSCAL s)
|
|
396
|
+
{
|
|
397
|
+
for (size_t i = 0; i < size_t(h)*size_t(w); i++) data[i] = s;
|
|
398
|
+
return *this;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
INLINE auto View() const { return FlatMatrix(*this); }
|
|
402
|
+
INLINE auto ViewRW() { return *this; }
|
|
403
|
+
INLINE tuple<size_t, size_t> Shape() const { return { h, w }; }
|
|
404
|
+
|
|
405
|
+
/// copy size and pointers
|
|
406
|
+
INLINE FlatMatrix & Assign (const FlatMatrix & m)
|
|
407
|
+
{
|
|
408
|
+
h = m.h;
|
|
409
|
+
w = m.w;
|
|
410
|
+
data = m.data;
|
|
411
|
+
return *this;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/// access operator, linear access
|
|
415
|
+
INLINE TELEM & operator() (size_t i) const
|
|
416
|
+
{
|
|
417
|
+
NETGEN_CHECK_RANGE(i, 0, Height()*Width());
|
|
418
|
+
return data[i];
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/// access operator
|
|
422
|
+
INLINE TELEM & operator() (size_t i, size_t j) const
|
|
423
|
+
{
|
|
424
|
+
NETGEN_CHECK_RANGE(i, 0, Height());
|
|
425
|
+
NETGEN_CHECK_RANGE(j, 0, Width());
|
|
426
|
+
return data[j*size_t(h)+i];
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/// the height
|
|
430
|
+
INLINE size_t Height () const { return h; }
|
|
431
|
+
|
|
432
|
+
/// the width
|
|
433
|
+
INLINE size_t Width () const { return w; }
|
|
434
|
+
|
|
435
|
+
INLINE size_t Dist () const { return h; }
|
|
436
|
+
|
|
437
|
+
INLINE T * Data () const { return data; }
|
|
438
|
+
|
|
439
|
+
INLINE FlatVector<T> Col (size_t i) const
|
|
440
|
+
{
|
|
441
|
+
return FlatVector<T> (h, &data[i*size_t(h)]);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
INLINE SliceVector<T> Row (size_t i) const
|
|
445
|
+
{
|
|
446
|
+
return SliceVector<T> (w, h, &data[i]);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
SliceVector<T> Diag () const
|
|
450
|
+
{
|
|
451
|
+
return SliceVector<T> (h, w+1, &data[0]);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
INLINE FlatMatrix Cols (size_t first, size_t next) const
|
|
455
|
+
{
|
|
456
|
+
return FlatMatrix (h, next-first, data+first*h);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
INLINE FlatMatrix Cols (IntRange range) const
|
|
460
|
+
{
|
|
461
|
+
return FlatMatrix (h, range.Size(), data+range.First()*h);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
INLINE SliceMatrix<T,ColMajor> Rows (size_t first, size_t next) const
|
|
466
|
+
{
|
|
467
|
+
return SliceMatrix<T,ColMajor> (next-first, w, h, data+first);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
INLINE SliceMatrix<T,ColMajor> Rows (IntRange range) const
|
|
471
|
+
{
|
|
472
|
+
return SliceMatrix<T,ColMajor> (range.Size(), w, h, data+range.First());
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
INLINE auto SplitRows (size_t split) const
|
|
476
|
+
{
|
|
477
|
+
return tuple(Rows(0,split), Rows(split, Height()));
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
INLINE auto SplitCols (size_t split) const
|
|
481
|
+
{
|
|
482
|
+
return tuple(Cols(0,split), Cols(split, Width()));
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
#endif
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
A Matrix class with memory allocation/deallocation
|
|
499
|
+
*/
|
|
500
|
+
template <typename T, ORDERING ORD>
|
|
501
|
+
class Matrix : public FlatMatrix<T,ORD>
|
|
502
|
+
{
|
|
503
|
+
public:
|
|
504
|
+
|
|
505
|
+
/// element type
|
|
506
|
+
typedef T TELEM;
|
|
507
|
+
/// scalar type of elements (double or Complex)
|
|
508
|
+
typedef typename mat_traits<T>::TSCAL TSCAL;
|
|
509
|
+
|
|
510
|
+
/// default constructor
|
|
511
|
+
Matrix () : FlatMatrix<T,ORD> (0, 0, nullptr) { ; }
|
|
512
|
+
|
|
513
|
+
/// allocate matrix of size ah * ah
|
|
514
|
+
Matrix (size_t ah) : FlatMatrix<T,ORD> (ah, new T[ah*ah]) { ; }
|
|
515
|
+
|
|
516
|
+
/// allocate matrix of size ah * aw
|
|
517
|
+
Matrix (size_t ah, size_t aw) : FlatMatrix<T,ORD> (ah, aw, new T[ah*aw]) { ; }
|
|
518
|
+
|
|
519
|
+
/// allocate and copy matrix
|
|
520
|
+
INLINE Matrix (const Matrix & m2)
|
|
521
|
+
: FlatMatrix<T,ORD> (m2.Height(), m2.Width(), new T[m2.Height()*m2.Width()])
|
|
522
|
+
{
|
|
523
|
+
FlatMatrix<T,ORD>::operator= (m2);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/// move matrix
|
|
527
|
+
INLINE Matrix (Matrix && m2)
|
|
528
|
+
: FlatMatrix<T,ORD> (m2.h, m2.w, m2.data)
|
|
529
|
+
{ m2.data = nullptr; m2.w = 0; m2.h = 0; }
|
|
530
|
+
|
|
531
|
+
/// allocate and compute
|
|
532
|
+
template<typename TB>
|
|
533
|
+
INLINE Matrix (const Expr<TB> & m2)
|
|
534
|
+
: FlatMatrix<T,ORD> (m2.Height(), m2.Width(), new T[m2.Height()*m2.Width()])
|
|
535
|
+
{
|
|
536
|
+
MatExpr<FlatMatrix<T,ORD> >::operator= (m2); // .View()); // does it work with view ?
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
Matrix (initializer_list<initializer_list<T>> llist)
|
|
540
|
+
: FlatMatrix<T,ORD> (0,0,nullptr)
|
|
541
|
+
{
|
|
542
|
+
int h = llist.size();
|
|
543
|
+
int w = 0;
|
|
544
|
+
for (auto row : llist)
|
|
545
|
+
w = std::max(w, int(row.size()));
|
|
546
|
+
|
|
547
|
+
SetSize (h, w);
|
|
548
|
+
(*this) = T(0.0);
|
|
549
|
+
|
|
550
|
+
int r = 0;
|
|
551
|
+
for (auto row : llist)
|
|
552
|
+
{
|
|
553
|
+
int c = 0;
|
|
554
|
+
for (auto col : row)
|
|
555
|
+
(*this)(r,c++) = col;
|
|
556
|
+
r++;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
/// delete memory
|
|
563
|
+
~Matrix() { delete [] this->data; }
|
|
564
|
+
|
|
565
|
+
template <typename ARCHIVE>
|
|
566
|
+
void DoArchive (ARCHIVE & ar)
|
|
567
|
+
{
|
|
568
|
+
ar & this->h & this->w;
|
|
569
|
+
if(ar.Input())
|
|
570
|
+
SetSize(this->h, this->w);
|
|
571
|
+
ar.Do(this->data, this->h*this->w);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/// sets new size of matrix
|
|
575
|
+
void SetSize(size_t ah, size_t aw)
|
|
576
|
+
{
|
|
577
|
+
if (this->h == ah && this->w == aw) return;
|
|
578
|
+
delete [] this->data;
|
|
579
|
+
this->h = ah;
|
|
580
|
+
this->w = aw;
|
|
581
|
+
this->data = new T[this->h * this->w];
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/// sets new size of matrix
|
|
585
|
+
void SetSize(size_t ah)
|
|
586
|
+
{
|
|
587
|
+
if (this->h == ah && this->w == ah) return;
|
|
588
|
+
delete [] this->data;
|
|
589
|
+
this->h = ah;
|
|
590
|
+
this->w = ah;
|
|
591
|
+
this->data = new T[this->h * this->w];
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
void ChangeSize (size_t newh, size_t neww)
|
|
596
|
+
{
|
|
597
|
+
Matrix tmp(newh, neww);
|
|
598
|
+
auto minh = std::min(newh, this->Height());
|
|
599
|
+
auto minw = std::min(neww, this->Width());
|
|
600
|
+
tmp.Rows (minh).Cols(minw) = this->Rows(minh).Cols(minw);
|
|
601
|
+
(*this) = std::move(tmp);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/// assign matrix, sizes must match
|
|
605
|
+
template<typename TB>
|
|
606
|
+
INLINE Matrix & operator= (const Expr<TB> & m)
|
|
607
|
+
{
|
|
608
|
+
SetSize (m.Height(), m.Width());
|
|
609
|
+
MatExpr<FlatMatrix<T,ORD> >::operator= (m);
|
|
610
|
+
return *this;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
/// fill matrix with scalar
|
|
615
|
+
Matrix & operator= (TSCAL s)
|
|
616
|
+
{
|
|
617
|
+
FlatMatrix<T,ORD>::operator= (s);
|
|
618
|
+
return *this;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
/// fill matrix with scalar
|
|
623
|
+
Matrix & operator= (const Matrix & m2)
|
|
624
|
+
{
|
|
625
|
+
SetSize (m2.Height(), m2.Width());
|
|
626
|
+
FlatMatrix<T,ORD>::operator= (m2);
|
|
627
|
+
return *this;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
Matrix & operator= (Matrix && m2)
|
|
631
|
+
{
|
|
632
|
+
this->h = m2.h;
|
|
633
|
+
this->w = m2.w;
|
|
634
|
+
ngcore::Swap (this->data, m2.data);
|
|
635
|
+
return *this;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
template<typename M2>
|
|
639
|
+
Matrix & operator+= (const M2 & m)
|
|
640
|
+
{
|
|
641
|
+
MatExpr<FlatMatrix<T,ORD> >::operator+= (m);
|
|
642
|
+
return *this;
|
|
643
|
+
}
|
|
644
|
+
};
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
A matrix of fixed size.
|
|
651
|
+
Useful as entry type in system matrices,...
|
|
652
|
+
*/
|
|
653
|
+
template <int H, int W = H, typename T = double>
|
|
654
|
+
class Mat : public MatExpr<Mat<H,W,T> >
|
|
655
|
+
{
|
|
656
|
+
// T data[(H*W>0) ? H*W : 1];
|
|
657
|
+
HTArray<H*W,T> data;
|
|
658
|
+
public:
|
|
659
|
+
typedef T TELEM;
|
|
660
|
+
typedef typename mat_traits<T>::TSCAL TSCAL;
|
|
661
|
+
typedef Vec<H, typename mat_traits<T>::TV_COL> TV_COL;
|
|
662
|
+
typedef Vec<W, typename mat_traits<T>::TV_ROW> TV_ROW;
|
|
663
|
+
static constexpr bool IsLinear() { return true; }
|
|
664
|
+
/// do not initialize
|
|
665
|
+
constexpr Mat () = default;
|
|
666
|
+
Mat (const Mat &) = default;
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
auto & HTData() const { return data; }
|
|
670
|
+
template <typename T2>
|
|
671
|
+
Mat (const Mat<H,W,T2> & m2) : data(m2.HTData()) { ; }
|
|
672
|
+
|
|
673
|
+
/// assign values
|
|
674
|
+
template<typename TB>
|
|
675
|
+
INLINE Mat (const Expr<TB> & m)
|
|
676
|
+
{
|
|
677
|
+
MatExpr<Mat>::operator= (m);
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/// fill with scalar
|
|
681
|
+
INLINE Mat (TSCAL s)
|
|
682
|
+
{
|
|
683
|
+
for (size_t i = 0; i < H*W; i++)
|
|
684
|
+
data[i] = s;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
Mat (initializer_list<initializer_list<T>> llist)
|
|
688
|
+
{
|
|
689
|
+
int r = 0;
|
|
690
|
+
for (auto row : llist)
|
|
691
|
+
{
|
|
692
|
+
int c = 0;
|
|
693
|
+
for (auto col : row)
|
|
694
|
+
(*this)(r,c++) = col;
|
|
695
|
+
r++;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
template <typename ...TTUP>
|
|
700
|
+
Mat (tuple<TTUP...> tup)
|
|
701
|
+
{
|
|
702
|
+
constexpr int s = tuple_size<decltype(tup)>();
|
|
703
|
+
Iterate<s> ([this, tup] (auto i) { this->data[i] = std::get<i>(tup); });
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
template <class... T2>
|
|
707
|
+
Mat (T v0, T v1, T2... rest)
|
|
708
|
+
: Mat(tuple(v0, v1, rest...)) { }
|
|
709
|
+
|
|
710
|
+
/// assign values
|
|
711
|
+
template<typename TB>
|
|
712
|
+
INLINE Mat & operator= (const Expr<TB> & m)
|
|
713
|
+
{
|
|
714
|
+
MatExpr<Mat>::operator= (m);
|
|
715
|
+
return *this;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/// copy matrix
|
|
719
|
+
Mat & operator= (const Mat &) = default;
|
|
720
|
+
/*
|
|
721
|
+
INLINE Mat & operator= (const Mat & m)
|
|
722
|
+
{
|
|
723
|
+
for (int i = 0; i < H*W; i++)
|
|
724
|
+
data[i] = m.data[i];
|
|
725
|
+
return *this;
|
|
726
|
+
}
|
|
727
|
+
*/
|
|
728
|
+
|
|
729
|
+
/// fill values
|
|
730
|
+
INLINE Mat & operator= (TSCAL s)
|
|
731
|
+
{
|
|
732
|
+
for (size_t i = 0; i < H*W; i++)
|
|
733
|
+
data[i] = s;
|
|
734
|
+
return *this;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
INLINE auto View() const { return Mat<H,W,const T>{*this}; }
|
|
738
|
+
INLINE auto & ViewRW() { return *this; }
|
|
739
|
+
INLINE auto Shape() const { return tuple(IC<H>(), IC<W>()); }
|
|
740
|
+
|
|
741
|
+
INLINE T* Data() noexcept { return data.Ptr(); }
|
|
742
|
+
INLINE const T* Data() const noexcept { return data.Ptr(); }
|
|
743
|
+
/// linear access
|
|
744
|
+
INLINE TELEM & operator() (size_t i) { return data[i]; }
|
|
745
|
+
/// access element
|
|
746
|
+
INLINE TELEM & operator() (size_t i, size_t j) { return data[i*W+j]; }
|
|
747
|
+
/// linear access
|
|
748
|
+
INLINE const TELEM & operator() (size_t i) const { return data[i]; }
|
|
749
|
+
/// access element
|
|
750
|
+
INLINE const TELEM & operator() (size_t i, size_t j) const { return data[i*W+j]; }
|
|
751
|
+
|
|
752
|
+
/// the height
|
|
753
|
+
INLINE static constexpr auto Height () { return IC<H>(); }
|
|
754
|
+
/// the width
|
|
755
|
+
INLINE static constexpr auto Width () { return IC<W>(); }
|
|
756
|
+
|
|
757
|
+
///
|
|
758
|
+
INLINE FlatVec<W,T> Row (size_t i)
|
|
759
|
+
{
|
|
760
|
+
return FlatVec<W,T> (&(*this)(i,0));
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
///
|
|
764
|
+
INLINE FlatVec<W,const T> Row (size_t i) const
|
|
765
|
+
{
|
|
766
|
+
return FlatVec<W,const T> (&(*this)(i,0));
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
///
|
|
770
|
+
INLINE auto Col (size_t i)
|
|
771
|
+
{
|
|
772
|
+
// return FixSliceVector<W,T> (H, &(*this)(0,i));
|
|
773
|
+
return FlatSliceVec<H,W,T> (&(*this)(0,i));
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
///
|
|
777
|
+
/*
|
|
778
|
+
INLINE const FixSliceVector<W,const T> Col (size_t i) const
|
|
779
|
+
{
|
|
780
|
+
return FixSliceVector<W,const T> (H, &(*this)(0,i));
|
|
781
|
+
}
|
|
782
|
+
*/
|
|
783
|
+
INLINE const FlatSliceVec<H,W,const T> Col (size_t i) const
|
|
784
|
+
{
|
|
785
|
+
return FlatSliceVec<H,W,const T> (&(*this)(0,i));
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
INLINE auto Rows (size_t i, size_t j)
|
|
789
|
+
{
|
|
790
|
+
return MatrixView<T, RowMajor, size_t, IC<W>, IC<W>> (j-i, IC<W>(), IC<W>(), Data()+i*Width());
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
INLINE auto Rows (IntRange r)
|
|
794
|
+
{
|
|
795
|
+
return Rows (r.First(), r.Next());
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
INLINE auto Cols (size_t i, size_t j)
|
|
799
|
+
{
|
|
800
|
+
return MatrixView<T, RowMajor, IC<H>, size_t, IC<W>> (IC<H>(), j-i, IC<W>(), Data()+i);
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
INLINE auto Cols (IntRange r)
|
|
804
|
+
{
|
|
805
|
+
return Cols (r.First(), r.Next());
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
INLINE auto Rows (size_t i, size_t j) const
|
|
809
|
+
{
|
|
810
|
+
return MatrixView<const T, RowMajor, size_t, IC<W>, IC<W>> (j-i, IC<W>(), IC<W>(), Data()+i*Width());
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
INLINE auto Rows (IntRange r) const
|
|
814
|
+
{
|
|
815
|
+
return Rows (r.First(), r.Next());
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
INLINE auto Cols (size_t i, size_t j) const
|
|
819
|
+
{
|
|
820
|
+
return MatrixView<const T, RowMajor, IC<H>, size_t, IC<W>> (IC<H>(), j-i, IC<W>(), Data()+i);
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
INLINE auto Cols (IntRange r) const
|
|
824
|
+
{
|
|
825
|
+
return Cols (r.First(), r.Next());
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
auto AsVector()
|
|
829
|
+
{
|
|
830
|
+
return FlatVec<H*W,T> (data.Ptr());
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
auto AsVector() const
|
|
834
|
+
{
|
|
835
|
+
return FlatVec<H*W,const T> (data.Ptr());
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
template <typename ARCHIVE>
|
|
839
|
+
void DoArchive(ARCHIVE& ar)
|
|
840
|
+
{
|
|
841
|
+
ar.Do(&data[0], H*W);
|
|
842
|
+
}
|
|
843
|
+
};
|
|
844
|
+
|
|
845
|
+
template <int H, int W, typename T>
|
|
846
|
+
class mat_traits<Mat<H,W,T>>
|
|
847
|
+
{
|
|
848
|
+
public:
|
|
849
|
+
/// matrix element
|
|
850
|
+
typedef T TELEM;
|
|
851
|
+
/// field of matrix element
|
|
852
|
+
typedef typename mat_traits<T>::TSCAL TSCAL;
|
|
853
|
+
/// type of column vector
|
|
854
|
+
typedef typename Mat<H,W,T>::TV_COL TV_COL;
|
|
855
|
+
/// type of row vector
|
|
856
|
+
typedef typename Mat<H,W,T>::TV_ROW TV_ROW;
|
|
857
|
+
/// matrix height
|
|
858
|
+
// enum { HEIGHT = H };
|
|
859
|
+
static constexpr int HEIGHT = H;
|
|
860
|
+
/// matrix with
|
|
861
|
+
// enum { WIDTH = W };
|
|
862
|
+
static constexpr int WIDTH = W;
|
|
863
|
+
///
|
|
864
|
+
// enum { IS_COMPLEX = mat_traits<TSCAL>::IS_COMPLEX };
|
|
865
|
+
};
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
/**
|
|
873
|
+
A diagonal matrix of fixed size.
|
|
874
|
+
*/
|
|
875
|
+
template <int H, typename T = double>
|
|
876
|
+
class DiagMat : public MatExpr<DiagMat<H,T> >
|
|
877
|
+
{
|
|
878
|
+
T data[(H>0) ? H : 1];
|
|
879
|
+
public:
|
|
880
|
+
typedef T TELEM;
|
|
881
|
+
typedef typename mat_traits<T>::TSCAL TSCAL;
|
|
882
|
+
typedef Vec<H, typename mat_traits<T>::TV_COL> TV_COL;
|
|
883
|
+
typedef Vec<H, typename mat_traits<T>::TV_ROW> TV_ROW;
|
|
884
|
+
enum { HEIGHT = H };
|
|
885
|
+
enum { WIDTH = H };
|
|
886
|
+
|
|
887
|
+
/// do not initialize
|
|
888
|
+
DiagMat () { ; }
|
|
889
|
+
|
|
890
|
+
/// copy matrix
|
|
891
|
+
DiagMat (const DiagMat & m)
|
|
892
|
+
: MatExpr<DiagMat> ()
|
|
893
|
+
{
|
|
894
|
+
(*this) = m;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
/// assign values
|
|
898
|
+
template<typename TB>
|
|
899
|
+
DiagMat (const Expr<TB> & m)
|
|
900
|
+
{
|
|
901
|
+
(*this) = m;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
/// fill with scalar
|
|
905
|
+
DiagMat (TSCAL s)
|
|
906
|
+
{
|
|
907
|
+
for (int i = 0; i < H; i++)
|
|
908
|
+
data[i] = s;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
/// assign values
|
|
912
|
+
template<typename TB>
|
|
913
|
+
DiagMat & operator= (const Expr<TB> & m)
|
|
914
|
+
{
|
|
915
|
+
for (int i = 0; i < H; i++)
|
|
916
|
+
data[i] = m.Spec()(i,i);
|
|
917
|
+
return *this;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
/// copy matrix
|
|
921
|
+
DiagMat & operator= (const DiagMat & m)
|
|
922
|
+
{
|
|
923
|
+
for (int i = 0; i < H; i++)
|
|
924
|
+
data[i] = m.data[i];
|
|
925
|
+
return *this;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
/// fill values
|
|
929
|
+
DiagMat & operator= (TSCAL s)
|
|
930
|
+
{
|
|
931
|
+
for (int i = 0; i < H; i++)
|
|
932
|
+
data[i] = s;
|
|
933
|
+
return *this;
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
// auto View() const { return DiagMat<H,const T>(*this); }
|
|
937
|
+
INLINE auto View() const { return *this; }
|
|
938
|
+
INLINE tuple<size_t, size_t> Shape() const { return { H,H }; }
|
|
939
|
+
|
|
940
|
+
/// linear access
|
|
941
|
+
TELEM & operator() (int i) { return data[i]; }
|
|
942
|
+
/// access element
|
|
943
|
+
// TELEM & operator() (int i, int j) { return (i==j) ? data[i] : 0; }
|
|
944
|
+
/// linear access
|
|
945
|
+
TELEM operator() (int i) const { return data[i]; }
|
|
946
|
+
TELEM operator[] (int i) const { return data[i]; }
|
|
947
|
+
/// access element
|
|
948
|
+
TELEM operator() (int i, int j) const { return (i==j) ? data[i] : 0; }
|
|
949
|
+
|
|
950
|
+
/// the height
|
|
951
|
+
constexpr size_t Height () const { return H; }
|
|
952
|
+
/// the width
|
|
953
|
+
constexpr size_t Width () const { return H; }
|
|
954
|
+
|
|
955
|
+
static constexpr bool IsLinear() { return false; }
|
|
956
|
+
};
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
template <int W, typename T = double, int DIST=W>
|
|
961
|
+
using FlatMatrixFixWidth = MatrixView<T,RowMajor,size_t,IC<W>,IC<DIST>>;
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
/**
|
|
965
|
+
A Matrix class with memory allocation/deallocation
|
|
966
|
+
*/
|
|
967
|
+
template <int W, typename T = double>
|
|
968
|
+
class MatrixFixWidth : public FlatMatrixFixWidth<W,T>
|
|
969
|
+
{
|
|
970
|
+
public:
|
|
971
|
+
typedef typename mat_traits<T>::TSCAL TSCAL;
|
|
972
|
+
|
|
973
|
+
INLINE MatrixFixWidth () : FlatMatrixFixWidth<W,T> (0, nullptr) { ; }
|
|
974
|
+
|
|
975
|
+
/// allocate matrix of size ah * ah
|
|
976
|
+
INLINE MatrixFixWidth (int ah) : FlatMatrixFixWidth<W,T> (ah, new T[ah*W]) { ; }
|
|
977
|
+
|
|
978
|
+
/// delete memory
|
|
979
|
+
INLINE ~MatrixFixWidth() { delete [] this->data; }
|
|
980
|
+
|
|
981
|
+
/// sets new size of matrix
|
|
982
|
+
INLINE void SetSize(int ah)
|
|
983
|
+
{
|
|
984
|
+
if (this->h == ah) return;
|
|
985
|
+
delete [] this->data;
|
|
986
|
+
this->h = ah;
|
|
987
|
+
this->data = new T[this->h * W];
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
/// assign matrix, sizes must match
|
|
991
|
+
template<typename TB>
|
|
992
|
+
INLINE MatrixFixWidth & operator= (const Expr<TB> & m)
|
|
993
|
+
{
|
|
994
|
+
FlatMatrixFixWidth<W,T>::operator= (m);
|
|
995
|
+
return *this;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
INLINE MatrixFixWidth & operator= (const MatrixFixWidth & m)
|
|
999
|
+
{
|
|
1000
|
+
FlatMatrixFixWidth<W,T>::operator= (m);
|
|
1001
|
+
return *this;
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
INLINE MatrixFixWidth & operator= (MatrixFixWidth && m)
|
|
1006
|
+
{
|
|
1007
|
+
this->h = m.h;
|
|
1008
|
+
ngcore::Swap (this->data, m.data);
|
|
1009
|
+
return *this;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
/// fill matrix with scalar
|
|
1013
|
+
INLINE MatrixFixWidth & operator= (TSCAL s)
|
|
1014
|
+
{
|
|
1015
|
+
FlatMatrixFixWidth<W,T>::operator= (s);
|
|
1016
|
+
return *this;
|
|
1017
|
+
}
|
|
1018
|
+
};
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
/**
|
|
1027
|
+
A Matrix which height is known at compile time
|
|
1028
|
+
No memory allocation/deallocation. User must provide memory.
|
|
1029
|
+
Matrix is stored colum-wise
|
|
1030
|
+
*/
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
template <int H, typename T = double, int DIST=H>
|
|
1034
|
+
using FlatMatrixFixHeight = MatrixView<T,ColMajor,IC<H>,size_t,IC<DIST>>;
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
/**
|
|
1038
|
+
A Matrix class with memory allocation/deallocation
|
|
1039
|
+
*/
|
|
1040
|
+
template <int H, typename T = double>
|
|
1041
|
+
class MatrixFixHeight : public FlatMatrixFixHeight<H,T>
|
|
1042
|
+
{
|
|
1043
|
+
public:
|
|
1044
|
+
typedef typename mat_traits<T>::TSCAL TSCAL;
|
|
1045
|
+
|
|
1046
|
+
/// allocate matrix of size ah * ah
|
|
1047
|
+
MatrixFixHeight (int aw) : FlatMatrixFixHeight<H,T> (aw, new T[aw*H]) { ; }
|
|
1048
|
+
|
|
1049
|
+
/// delete memory
|
|
1050
|
+
~MatrixFixHeight() { delete [] this->data; }
|
|
1051
|
+
|
|
1052
|
+
/// sets new size of matrix
|
|
1053
|
+
void SetSize(int aw)
|
|
1054
|
+
{
|
|
1055
|
+
if (this->w == aw) return;
|
|
1056
|
+
delete [] this->data;
|
|
1057
|
+
this->w = aw;
|
|
1058
|
+
this->data = new T[H*this->w];
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
/// assign matrix, sizes must match
|
|
1062
|
+
template<typename TB>
|
|
1063
|
+
MatrixFixHeight & operator= (const Expr<TB> & m)
|
|
1064
|
+
{
|
|
1065
|
+
int k = 0;
|
|
1066
|
+
for (int j = 0; j < this->w; j++)
|
|
1067
|
+
for (int i = 0; i < H; i++)
|
|
1068
|
+
{
|
|
1069
|
+
this->data[k] = m.Spec()(i,j);
|
|
1070
|
+
k++;
|
|
1071
|
+
}
|
|
1072
|
+
return *this;
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
/// fill matrix with scalar
|
|
1076
|
+
MatrixFixHeight & operator= (TSCAL s)
|
|
1077
|
+
{
|
|
1078
|
+
FlatMatrixFixHeight<H,T>::operator= (s);
|
|
1079
|
+
return *this;
|
|
1080
|
+
}
|
|
1081
|
+
};
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
template <typename T, ORDERING ORD, typename TH, typename TW, typename TDIST>
|
|
1088
|
+
class MatrixView : public MatExpr<MatrixView<T,ORD,TH,TW,TDIST>>
|
|
1089
|
+
{
|
|
1090
|
+
protected:
|
|
1091
|
+
typedef MatExpr<MatrixView<T,ORD,TH,TW,TDIST>> BASE;
|
|
1092
|
+
/// the height
|
|
1093
|
+
NO_UNIQUE_ADDRESS TH h;
|
|
1094
|
+
/// the width
|
|
1095
|
+
NO_UNIQUE_ADDRESS TW w;
|
|
1096
|
+
/// the distance
|
|
1097
|
+
NO_UNIQUE_ADDRESS TDIST dist;
|
|
1098
|
+
/// the data
|
|
1099
|
+
T * __restrict data;
|
|
1100
|
+
public:
|
|
1101
|
+
|
|
1102
|
+
/// element type
|
|
1103
|
+
typedef T TELEM;
|
|
1104
|
+
/// scalar type of elements (double or Complex)
|
|
1105
|
+
typedef typename mat_traits<T>::TSCAL TSCAL;
|
|
1106
|
+
static constexpr bool IsLinear() { return false; }
|
|
1107
|
+
//
|
|
1108
|
+
MatrixView() = default;
|
|
1109
|
+
// : h{0}, w{0}, dist{0}, data{nullptr} { }
|
|
1110
|
+
INLINE MatrixView(const MatrixView &) = default;
|
|
1111
|
+
|
|
1112
|
+
template <typename T2, typename TH2, typename TW2, typename TDIST2,
|
|
1113
|
+
enable_if_t<is_convertible<T2*,T*>::value, int> =0,
|
|
1114
|
+
enable_if_t<is_constructible<TH,TH2>::value, int> =0,
|
|
1115
|
+
enable_if_t<is_constructible<TW,TW2>::value, int> =0,
|
|
1116
|
+
enable_if_t<is_constructible<TDIST,TDIST2>::value, int> =0>
|
|
1117
|
+
INLINE MatrixView (const MatrixView<T2, ORD, TH2, TW2, TDIST2> & m2)
|
|
1118
|
+
: h{TH(m2.Height())}, w{TW(m2.Width())}, dist{TDIST(m2.Dist())}, data{m2.Data()} { }
|
|
1119
|
+
|
|
1120
|
+
template <typename T2, typename TH2, typename TW2, typename TDIST2,
|
|
1121
|
+
enable_if_t<is_convertible<T2*,T*>::value, int> =0,
|
|
1122
|
+
enable_if_t<is_constructible<TH,TH2>::value, int> =0,
|
|
1123
|
+
enable_if_t<is_constructible<TW,TW2>::value, int> =0,
|
|
1124
|
+
enable_if_t<is_same<unused_dist,TDIST2>::value, int> =0>
|
|
1125
|
+
INLINE MatrixView (const MatrixView<T2, ORD, TH2, TW2, TDIST2> & m2)
|
|
1126
|
+
: h{TH(m2.Height())}, w{TW(m2.Width())}, dist{TDIST(m2.Dist())}, data{m2.Data()} { }
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
/// set height, width, dist, and mem
|
|
1130
|
+
INLINE MatrixView (TH ah, TW aw, TDIST adist, T * adata)
|
|
1131
|
+
: h(ah), w(aw), dist(adist), data(adata) { ; }
|
|
1132
|
+
|
|
1133
|
+
/// set height, width, dist, and mem
|
|
1134
|
+
INLINE MatrixView (TH ah, TW aw, T * adata)
|
|
1135
|
+
: h(ah), w(aw), data(adata)
|
|
1136
|
+
{
|
|
1137
|
+
static_assert(std::is_same<TDIST, unused_dist>(), "MatrixView needs dist");
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
INLINE MatrixView (size_t ah, size_t aw, LocalHeap & lh)
|
|
1141
|
+
: h(ah), w(aw), data (lh.Alloc<T>(ah*aw))
|
|
1142
|
+
{
|
|
1143
|
+
static_assert(std::is_same<TDIST, unused_dist>(), "MatrixView(lh) needs dist");
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
template<typename TB>
|
|
1147
|
+
INLINE MatrixView (const LocalHeapExpr<TB> & m2)
|
|
1148
|
+
{
|
|
1149
|
+
static_assert(std::is_same<TDIST, unused_dist>(), "MatrixView(lh-expr) needs dist");
|
|
1150
|
+
h = m2.A().Height();
|
|
1151
|
+
w = m2.A().Width();
|
|
1152
|
+
LocalHeap & lh = m2.GetLocalHeap();
|
|
1153
|
+
data = lh.Alloc<T> (h*w);
|
|
1154
|
+
this->operator= (m2.A());
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
INLINE MatrixView (size_t s, T * adata)
|
|
1159
|
+
: data(adata)
|
|
1160
|
+
{
|
|
1161
|
+
if constexpr (is_IC<TW>() && is_IC<TDIST>())
|
|
1162
|
+
h = s;
|
|
1163
|
+
else if constexpr (is_IC<TH>() && is_IC<TDIST>())
|
|
1164
|
+
w = s;
|
|
1165
|
+
else if constexpr (is_IC<TH>() && is_IC<TW>())
|
|
1166
|
+
dist = s;
|
|
1167
|
+
else if constexpr (is_same<TDIST,unused_dist>())
|
|
1168
|
+
h = w = s;
|
|
1169
|
+
// else
|
|
1170
|
+
// static_assert(false, "illegal 1-size ctor of MatrixView");
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
INLINE MatrixView (size_t s, LocalHeap & lh)
|
|
1174
|
+
{
|
|
1175
|
+
if constexpr (is_IC<TW>() && is_IC<TDIST>())
|
|
1176
|
+
h = s;
|
|
1177
|
+
else if constexpr (is_IC<TH>() && is_IC<TDIST>())
|
|
1178
|
+
w = s;
|
|
1179
|
+
else if constexpr (is_IC<TH>() && is_IC<TW>())
|
|
1180
|
+
dist = s;
|
|
1181
|
+
else if constexpr (is_same<TDIST,unused_dist>())
|
|
1182
|
+
h = w = s;
|
|
1183
|
+
|
|
1184
|
+
data = lh.Alloc<T>(h*w);
|
|
1185
|
+
// else
|
|
1186
|
+
// static_assert(false, "illegal 1-size ctor of MatrixView");
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
|
|
1190
|
+
template <typename T2>
|
|
1191
|
+
static auto ICOrVal (size_t val)
|
|
1192
|
+
{
|
|
1193
|
+
if constexpr (is_IC<T2>())
|
|
1194
|
+
return T2();
|
|
1195
|
+
else
|
|
1196
|
+
return val;
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
/*
|
|
1200
|
+
MatrixView (FlatMatrix<T,ORD> mat)
|
|
1201
|
+
: h(ICOrVal<TH>(mat.Height())),
|
|
1202
|
+
w(ICOrVal<TW>(mat.Width())),
|
|
1203
|
+
dist(ICOrVal<TDIST>(mat.Dist())), data(mat.Data())
|
|
1204
|
+
{ ; }
|
|
1205
|
+
*/
|
|
1206
|
+
|
|
1207
|
+
/*
|
|
1208
|
+
template<int W, int DIST>
|
|
1209
|
+
MatrixView (FlatMatrixFixInner<W,T,DIST,ORD> mat)
|
|
1210
|
+
: h(mat.Height()), w(mat.Width()), dist(mat.Dist()), data(mat.Data())
|
|
1211
|
+
{ ; }
|
|
1212
|
+
*/
|
|
1213
|
+
|
|
1214
|
+
template<int H, int W>
|
|
1215
|
+
INLINE MatrixView (const Mat<H,W,T> & mat)
|
|
1216
|
+
: h(mat.Height()), w(mat.Width()), dist(mat.Width()), data(const_cast<T*>(mat.Data()))
|
|
1217
|
+
{
|
|
1218
|
+
static_assert(ORD==RowMajor);
|
|
1219
|
+
}
|
|
1220
|
+
/*
|
|
1221
|
+
template<int H, int W>
|
|
1222
|
+
INLINE MatrixView(const Mat<H,W,const T> & mat)
|
|
1223
|
+
: h(mat.Height()), w(mat.Width()), dist(mat.Width()), data(const_cast<T*>(mat.Data()))
|
|
1224
|
+
{
|
|
1225
|
+
static_assert(ORD==RowMajor);
|
|
1226
|
+
}
|
|
1227
|
+
*/
|
|
1228
|
+
|
|
1229
|
+
|
|
1230
|
+
/// assign contents
|
|
1231
|
+
template<typename TB>
|
|
1232
|
+
INLINE MatrixView & operator= (const Expr<TB> & m)
|
|
1233
|
+
{
|
|
1234
|
+
BASE::operator= (m);
|
|
1235
|
+
return *this;
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
INLINE MatrixView & operator= (const MatrixView & m)
|
|
1239
|
+
{
|
|
1240
|
+
BASE::operator= (m);
|
|
1241
|
+
return *this;
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
/// assign constant
|
|
1245
|
+
INLINE MatrixView & operator= (TSCAL s)
|
|
1246
|
+
{
|
|
1247
|
+
if (InnerSize() == 0) return *this;
|
|
1248
|
+
for (size_t i = 0; i < OuterSize(); i++)
|
|
1249
|
+
for (size_t j = 0; j < InnerSize(); j++)
|
|
1250
|
+
data[i*Dist()+j] = s;
|
|
1251
|
+
return *this;
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
INLINE auto View() const { return *this; }
|
|
1255
|
+
INLINE auto ViewRW() { return *this; }
|
|
1256
|
+
INLINE auto Shape() const { return tuple(h,w); }
|
|
1257
|
+
|
|
1258
|
+
INLINE TELEM * Addr(size_t i, size_t j) const
|
|
1259
|
+
{
|
|
1260
|
+
if constexpr (ORD==RowMajor)
|
|
1261
|
+
return data+i*Dist()+j;
|
|
1262
|
+
else
|
|
1263
|
+
return data+j*Dist()+i;
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
/// access operator
|
|
1268
|
+
INLINE TELEM & operator() (size_t i, size_t j) const
|
|
1269
|
+
{
|
|
1270
|
+
NETGEN_CHECK_RANGE(i, 0, Height());
|
|
1271
|
+
NETGEN_CHECK_RANGE(j, 0, Width());
|
|
1272
|
+
return *Addr(i,j);
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
/// access operator, linear access
|
|
1276
|
+
INLINE TELEM & operator() (size_t i) const
|
|
1277
|
+
{
|
|
1278
|
+
NETGEN_CHECK_RANGE(i, 0, Height()*Dist());
|
|
1279
|
+
return data[i];
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
INLINE auto RowDist() const
|
|
1283
|
+
{
|
|
1284
|
+
if constexpr (ORD==RowMajor)
|
|
1285
|
+
return IC<1>();
|
|
1286
|
+
else
|
|
1287
|
+
return Dist();
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
INLINE auto ColDist() const
|
|
1291
|
+
{
|
|
1292
|
+
if constexpr (ORD==RowMajor)
|
|
1293
|
+
return Dist();
|
|
1294
|
+
else
|
|
1295
|
+
return IC<1>();
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
|
|
1299
|
+
INLINE auto Height () const { return h; }
|
|
1300
|
+
INLINE auto Width () const { return w; }
|
|
1301
|
+
INLINE auto Dist () const
|
|
1302
|
+
{
|
|
1303
|
+
if constexpr (std::is_same<TDIST, unused_dist>())
|
|
1304
|
+
{
|
|
1305
|
+
if constexpr (ORD==RowMajor)
|
|
1306
|
+
return Width();
|
|
1307
|
+
else
|
|
1308
|
+
return Height();
|
|
1309
|
+
}
|
|
1310
|
+
else
|
|
1311
|
+
return dist;
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
INLINE T* Data() const { return data; }
|
|
1315
|
+
INLINE auto InnerSize () const
|
|
1316
|
+
{
|
|
1317
|
+
if constexpr (ORD==RowMajor)
|
|
1318
|
+
return Width();
|
|
1319
|
+
else
|
|
1320
|
+
return Height();
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
INLINE auto OuterSize () const
|
|
1324
|
+
{
|
|
1325
|
+
if constexpr (ORD==RowMajor)
|
|
1326
|
+
return Height();
|
|
1327
|
+
else
|
|
1328
|
+
return Width();
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
using BASE::Rows;
|
|
1332
|
+
using BASE::Cols;
|
|
1333
|
+
|
|
1334
|
+
MatrixView<T,ORD> AddSize (size_t h, size_t w) const
|
|
1335
|
+
{
|
|
1336
|
+
NETGEN_CHECK_RANGE(h, Height(), Height()+1);
|
|
1337
|
+
NETGEN_CHECK_RANGE(w, Width(), Width()+1);
|
|
1338
|
+
return MatrixView<T,ORD,size_t,size_t,size_t> (h, w, dist, data);
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
INLINE auto SplitRows (size_t split) const
|
|
1342
|
+
{
|
|
1343
|
+
return tuple(Rows(0,split), Rows(split, Height()));
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
INLINE auto SplitCols (size_t split) const
|
|
1347
|
+
{
|
|
1348
|
+
return tuple(Cols(0,split), Cols(split, Width()));
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
INLINE auto Row (size_t i) const
|
|
1352
|
+
{
|
|
1353
|
+
return VectorView(Width(), RowDist(), Addr(i,0));
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
INLINE auto Col (size_t i) const
|
|
1357
|
+
{
|
|
1358
|
+
return VectorView(Height(), ColDist(), Addr(0,i));
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
INLINE auto Cols (size_t first, size_t next) const
|
|
1362
|
+
{
|
|
1363
|
+
if constexpr (ORD==RowMajor)
|
|
1364
|
+
return MatrixView<T,ORD,TH,size_t,size_t> (h, next-first, Dist(), Addr(0,first));
|
|
1365
|
+
else
|
|
1366
|
+
return MatrixView<T,ORD,TH,size_t,TDIST> (h, next-first, Dist(), Addr(0,first));
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
INLINE auto Cols (size_t next) const
|
|
1370
|
+
{
|
|
1371
|
+
return Cols(0, next);
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
|
|
1375
|
+
INLINE auto Rows (size_t first, size_t next) const
|
|
1376
|
+
{
|
|
1377
|
+
if constexpr (ORD==RowMajor)
|
|
1378
|
+
return MatrixView<T,ORD,size_t,TW,TDIST> (next-first, w, Dist(), Addr(first, 0));
|
|
1379
|
+
else
|
|
1380
|
+
return MatrixView<T,ORD,size_t,TW,size_t> (next-first, w, Dist(), Addr(first, 0));
|
|
1381
|
+
}
|
|
1382
|
+
|
|
1383
|
+
INLINE auto Rows (IntRange range) const
|
|
1384
|
+
{
|
|
1385
|
+
return Rows (range.First(), range.Next());
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
INLINE auto Rows (size_t next) const
|
|
1389
|
+
{
|
|
1390
|
+
return Rows (0, next);
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
template <int R>
|
|
1394
|
+
INLINE auto Rows() const
|
|
1395
|
+
{
|
|
1396
|
+
if constexpr (ORD==RowMajor)
|
|
1397
|
+
return MatrixView<T,ORD,IC<R>,TW,TDIST> (IC<R>(), w, Dist(), Addr(0,0));
|
|
1398
|
+
else
|
|
1399
|
+
return MatrixView<T,ORD,IC<R>,TW,size_t> (IC<R>(), w, Dist(), Addr(0,0));
|
|
1400
|
+
}
|
|
1401
|
+
|
|
1402
|
+
INLINE auto Cols (IntRange range) const
|
|
1403
|
+
{
|
|
1404
|
+
return Cols (range.First(), range.Next());
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
/*
|
|
1408
|
+
///
|
|
1409
|
+
INLINE operator const FlatMatrix<T,ORD>() const
|
|
1410
|
+
{
|
|
1411
|
+
// static_assert( Dist() == InnerSize() );
|
|
1412
|
+
return FlatMatrix<T,ORD> (Height(), Width(), data);
|
|
1413
|
+
}
|
|
1414
|
+
*/
|
|
1415
|
+
|
|
1416
|
+
auto AsVector() const
|
|
1417
|
+
{
|
|
1418
|
+
// static_assert( Dist() == InnerSize() );
|
|
1419
|
+
return FlatVector<T> (h*w, data);
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
auto Bare() const
|
|
1423
|
+
{
|
|
1424
|
+
return make_BareSliceMatrix(*this);
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
INLINE auto RemoveConst() const
|
|
1428
|
+
{ return MatrixView<typename remove_const<T>::type,ORD,TH,TW,TDIST>
|
|
1429
|
+
(h,w,dist, const_cast<typename remove_const<T>::type*> (data)); }
|
|
1430
|
+
|
|
1431
|
+
void IncPtr (size_t inc) { data += inc; } // should be only for bare
|
|
1432
|
+
|
|
1433
|
+
|
|
1434
|
+
const auto RowSlice(size_t first, size_t adist) const
|
|
1435
|
+
{
|
|
1436
|
+
static_assert (ORD==RowMajor);
|
|
1437
|
+
// NETGEN_CHECK_RANGE(first, 0, Height()); // too restrictive
|
|
1438
|
+
NETGEN_CHECK_RANGE(first, 0, adist);
|
|
1439
|
+
return BareSliceMatrix<T,ORD> (Height()/adist, Width(), Dist()*adist, data+first*Dist());
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
|
|
1443
|
+
INLINE SliceVector<T> Diag () const
|
|
1444
|
+
{
|
|
1445
|
+
if (ORD == RowMajor)
|
|
1446
|
+
return SliceVector<T> (h, Dist()+1, data);
|
|
1447
|
+
else
|
|
1448
|
+
return SliceVector<T> (w, Dist()+1, data);
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
INLINE SliceVector<T> Diag (int offset) const
|
|
1452
|
+
{
|
|
1453
|
+
// return SliceVector<T> (h, dist+1, data);
|
|
1454
|
+
int dp = std::max(offset, 0);
|
|
1455
|
+
int dm = std::min(offset, 0);
|
|
1456
|
+
if (ORD == RowMajor)
|
|
1457
|
+
return SliceVector<T> (min(w-dp, h+dm), Dist()+1, data+dp-dm*Dist());
|
|
1458
|
+
else
|
|
1459
|
+
return SliceVector<T> (min(w-dp, h+dm), Dist()+1, data-dm+dp*Dist());
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
// [[deprecated("Use placenement ctor: new(ptr) Matrix(n, lh); instead!")]]
|
|
1463
|
+
void AssignMemory (size_t s, LocalHeap & lh)
|
|
1464
|
+
{
|
|
1465
|
+
new (this) MatrixView(s, lh);
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
void AssignMemory (size_t ah, size_t aw, T * data)
|
|
1469
|
+
{
|
|
1470
|
+
static_assert(std::is_same<TDIST, unused_dist>(), "MatrixView::AssignMemory needs unused-dist");
|
|
1471
|
+
new (this) MatrixView(ah, aw, data);
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
void AssignMemory (size_t ah, size_t aw, LocalHeap & lh)
|
|
1475
|
+
{
|
|
1476
|
+
static_assert(std::is_same<TDIST, unused_dist>(), "MatrixView::AssignMemory needs unused-dist");
|
|
1477
|
+
new (this) MatrixView(ah, aw, lh);
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
auto Assign (const MatrixView & m)
|
|
1481
|
+
{
|
|
1482
|
+
data = m.data;
|
|
1483
|
+
h = m.h;
|
|
1484
|
+
w = m.w;
|
|
1485
|
+
dist = m.dist;
|
|
1486
|
+
return *this;
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
auto Swap (MatrixView & m)
|
|
1490
|
+
{
|
|
1491
|
+
ngcore::Swap (data, m.data);
|
|
1492
|
+
ngcore::Swap (h, m.h);
|
|
1493
|
+
ngcore::Swap (w, m.w);
|
|
1494
|
+
ngcore::Swap (dist, m.dist);
|
|
1495
|
+
return *this;
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
auto Reshape(size_t h2, size_t w2)
|
|
1499
|
+
{
|
|
1500
|
+
static_assert(std::is_same<TDIST, unused_dist>(), "MatrixView::Reshape needs unused-dist");
|
|
1501
|
+
return FlatMatrix<T,ORD>{h2,w2,data};
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
|
|
1505
|
+
// template <typename TH2, typename TW2>
|
|
1506
|
+
// operator MatrixView<T,ORD,TH2,TW2,size_t>() const { return { Height(), Width(), Dist(), Data() }; }
|
|
1507
|
+
};
|
|
1508
|
+
|
|
1509
|
+
|
|
1510
|
+
|
|
1511
|
+
|
|
1512
|
+
|
|
1513
|
+
|
|
1514
|
+
|
|
1515
|
+
|
|
1516
|
+
template <typename T, ORDERING ORDER>
|
|
1517
|
+
SliceMatrix<T,ORDER> make_SliceMatrix (FlatMatrix<T,ORDER> mat) { return mat; }
|
|
1518
|
+
|
|
1519
|
+
template <int H, int W, typename T>
|
|
1520
|
+
SliceMatrix<T,RowMajor> make_SliceMatrix (const Mat<H,W,T> &mat) { return const_cast<Mat<H,W,T>&>(mat); }
|
|
1521
|
+
|
|
1522
|
+
/*
|
|
1523
|
+
template <typename T, ORDERING ORDER, typename TH, typename TW, typename TD>
|
|
1524
|
+
SliceMatrix<T,ORDER> make_SliceMatrix (MatrixView<T,ORDER,TH,TW,TD> mat) { return mat; }
|
|
1525
|
+
*/
|
|
1526
|
+
template <typename T, ORDERING ORDER, typename ...Args>
|
|
1527
|
+
SliceMatrix<T,ORDER> make_SliceMatrix (MatrixView<T, ORDER, Args...> mat) { return mat; }
|
|
1528
|
+
|
|
1529
|
+
|
|
1530
|
+
template <typename T, ORDERING ORDER>
|
|
1531
|
+
BareSliceMatrix<T,ORDER> make_BareSliceMatrix (FlatMatrix<T,ORDER> mat) { return mat; }
|
|
1532
|
+
|
|
1533
|
+
template <int H, int W, typename T>
|
|
1534
|
+
BareSliceMatrix<T,RowMajor> make_BareSliceMatrix (const Mat<H,W,T> &mat) { return const_cast<Mat<H,W,T>&>(mat); }
|
|
1535
|
+
|
|
1536
|
+
template <typename T, ORDERING ORDER, typename TH, typename TW, typename TDIST>
|
|
1537
|
+
BareSliceMatrix<T,ORDER> make_BareSliceMatrix (MatrixView<T,ORDER,TH,TW,TDIST> mat) { return mat; }
|
|
1538
|
+
|
|
1539
|
+
|
|
1540
|
+
|
|
1541
|
+
|
|
1542
|
+
|
|
1543
|
+
|
|
1544
|
+
|
|
1545
|
+
|
|
1546
|
+
template <typename T = double>
|
|
1547
|
+
class DoubleSliceMatrix : public MatExpr<DoubleSliceMatrix<T> >
|
|
1548
|
+
{
|
|
1549
|
+
protected:
|
|
1550
|
+
/// the height
|
|
1551
|
+
size_t h;
|
|
1552
|
+
/// the width
|
|
1553
|
+
size_t w;
|
|
1554
|
+
/// the distance
|
|
1555
|
+
size_t distr, distc;
|
|
1556
|
+
/// the data
|
|
1557
|
+
T * data;
|
|
1558
|
+
public:
|
|
1559
|
+
|
|
1560
|
+
/// element type
|
|
1561
|
+
typedef T TELEM;
|
|
1562
|
+
/// scalar type of elements (double or Complex)
|
|
1563
|
+
typedef typename mat_traits<T>::TSCAL TSCAL;
|
|
1564
|
+
|
|
1565
|
+
static constexpr bool IsLinear() { return false; }
|
|
1566
|
+
|
|
1567
|
+
/// set height, width, and mem
|
|
1568
|
+
DoubleSliceMatrix (int ah, int aw, int adistr, int adistc, T * adata)
|
|
1569
|
+
: h(ah), w(aw), distr(adistr), distc(adistc), data(adata) { ; }
|
|
1570
|
+
|
|
1571
|
+
/// assign contents
|
|
1572
|
+
template<typename TB>
|
|
1573
|
+
DoubleSliceMatrix & operator= (const Expr<TB> & m)
|
|
1574
|
+
{
|
|
1575
|
+
return MatExpr<DoubleSliceMatrix>::operator= (m);
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
/// assign constant
|
|
1579
|
+
DoubleSliceMatrix & operator= (TSCAL s)
|
|
1580
|
+
{
|
|
1581
|
+
for (size_t i = 0; i < h; i++)
|
|
1582
|
+
for (size_t j = 0; j < w; j++)
|
|
1583
|
+
data[i*distr+j*distc] = s;
|
|
1584
|
+
return *this;
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
INLINE auto View() const { return *this; }
|
|
1588
|
+
INLINE auto ViewRW() { return *this; }
|
|
1589
|
+
INLINE tuple<size_t,size_t> Shape() const { return { h, w }; }
|
|
1590
|
+
|
|
1591
|
+
/// access operator
|
|
1592
|
+
TELEM & operator() (size_t i, size_t j) const
|
|
1593
|
+
{
|
|
1594
|
+
NETGEN_CHECK_RANGE(i, 0, Height());
|
|
1595
|
+
NETGEN_CHECK_RANGE(j, 0, Width());
|
|
1596
|
+
return data[i*distr+j*distc];
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
/// access operator, linear access
|
|
1600
|
+
TELEM & operator() (size_t i) const
|
|
1601
|
+
{
|
|
1602
|
+
NETGEN_CHECK_RANGE(i, 0, Height()*Width());
|
|
1603
|
+
return data[i];
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
size_t Height () const { return h; }
|
|
1607
|
+
size_t Width () const { return w; }
|
|
1608
|
+
size_t DistRow () const { return distr; }
|
|
1609
|
+
size_t DistCol () const { return distc; }
|
|
1610
|
+
T * Data () const { return data; }
|
|
1611
|
+
|
|
1612
|
+
const DoubleSliceMatrix Rows (size_t first, size_t next) const
|
|
1613
|
+
{
|
|
1614
|
+
return DoubleSliceMatrix (next-first, w, distr, distc, data+first*distr);
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
const DoubleSliceMatrix<T> Cols (size_t first, size_t next) const
|
|
1618
|
+
{
|
|
1619
|
+
return DoubleSliceMatrix<T> (h, next-first, distr, distc, data+first*distc);
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
const DoubleSliceMatrix Rows (IntRange range) const
|
|
1623
|
+
{
|
|
1624
|
+
return Rows(range.First(), range.Next());
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
const DoubleSliceMatrix<T> Cols (IntRange range) const
|
|
1628
|
+
{
|
|
1629
|
+
return Cols(range.First(), range.Next());
|
|
1630
|
+
}
|
|
1631
|
+
};
|
|
1632
|
+
|
|
1633
|
+
|
|
1634
|
+
template <typename T>
|
|
1635
|
+
constexpr bool IsTrivialTranspose () { return IsScalar<T>(); }
|
|
1636
|
+
|
|
1637
|
+
|
|
1638
|
+
constexpr ORDERING operator! (ORDERING ordering)
|
|
1639
|
+
{
|
|
1640
|
+
return ordering == ColMajor ? RowMajor : ColMajor;
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
|
|
1644
|
+
|
|
1645
|
+
template <typename T, ORDERING ord,
|
|
1646
|
+
typename enable_if<IsTrivialTranspose<T>(),int>::type = 0>
|
|
1647
|
+
INLINE auto Trans (FlatMatrix<T,ord> mat)
|
|
1648
|
+
{
|
|
1649
|
+
return FlatMatrix<T,!ord> (mat.Width(), mat.Height(), mat.Data());
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
template <typename T, ORDERING ord,
|
|
1653
|
+
typename enable_if<IsTrivialTranspose<T>(),int>::type = 0>
|
|
1654
|
+
INLINE auto Trans (const Matrix<T,ord> & mat)
|
|
1655
|
+
{
|
|
1656
|
+
return FlatMatrix<T,!ord> (mat.Width(), mat.Height(), mat.Data());
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
|
|
1660
|
+
template <typename T, ORDERING ord, typename TH, typename TW, typename TD,
|
|
1661
|
+
typename enable_if<IsTrivialTranspose<T>(),int>::type = 0>
|
|
1662
|
+
INLINE const auto Trans (MatrixView<T,ord,TH,TW,TD> mat)
|
|
1663
|
+
{
|
|
1664
|
+
return MatrixView<T,!ord,TW,TH,TD> (mat.Width(), mat.Height(), mat.Dist(), mat.Data());
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
|
|
1668
|
+
|
|
1669
|
+
|
|
1670
|
+
|
|
1671
|
+
|
|
1672
|
+
template <class TM, class TSCAL>
|
|
1673
|
+
class Scalar2ElemMatrix
|
|
1674
|
+
{
|
|
1675
|
+
public:
|
|
1676
|
+
const BareSliceMatrix<TSCAL> mat;
|
|
1677
|
+
Scalar2ElemMatrix (const BareSliceMatrix<TSCAL> amat) : mat(amat) { ; }
|
|
1678
|
+
|
|
1679
|
+
/*
|
|
1680
|
+
enum { H = mat_traits<TM>::HEIGHT };
|
|
1681
|
+
enum { W = mat_traits<TM>::WIDTH };
|
|
1682
|
+
*/
|
|
1683
|
+
enum { H = Height<TM>() };
|
|
1684
|
+
enum { W = Width<TM>() };
|
|
1685
|
+
|
|
1686
|
+
TM operator() (size_t i, size_t j) const
|
|
1687
|
+
{
|
|
1688
|
+
/*
|
|
1689
|
+
TM ret;
|
|
1690
|
+
for (size_t k = 0; k < H; k++)
|
|
1691
|
+
for (size_t l = 0; l < W; l++)
|
|
1692
|
+
Access(ret, k,l) = mat(i*H+k, j*W+l);
|
|
1693
|
+
return ret;
|
|
1694
|
+
*/
|
|
1695
|
+
if constexpr (IsScalar<TM>())
|
|
1696
|
+
{
|
|
1697
|
+
return mat(i,j);
|
|
1698
|
+
}
|
|
1699
|
+
else
|
|
1700
|
+
{
|
|
1701
|
+
TM ret;
|
|
1702
|
+
for (size_t k = 0; k < H; k++)
|
|
1703
|
+
for (size_t l = 0; l < W; l++)
|
|
1704
|
+
ret(k,l) = mat(i*H+k, j*W+l);
|
|
1705
|
+
return ret;
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
Scalar2ElemMatrix Rows(size_t first, size_t next) const
|
|
1710
|
+
{
|
|
1711
|
+
return Scalar2ElemMatrix(mat.Rows(H*first, H*next));
|
|
1712
|
+
}
|
|
1713
|
+
};
|
|
1714
|
+
|
|
1715
|
+
|
|
1716
|
+
|
|
1717
|
+
|
|
1718
|
+
|
|
1719
|
+
|
|
1720
|
+
/**
|
|
1721
|
+
Identity Matrix of fixed size
|
|
1722
|
+
*/
|
|
1723
|
+
template <size_t H>
|
|
1724
|
+
class Id : public MatExpr<Id<H> >
|
|
1725
|
+
{
|
|
1726
|
+
public:
|
|
1727
|
+
typedef double TELEM;
|
|
1728
|
+
typedef double TSCAL;
|
|
1729
|
+
typedef Vec<H, double> TV_COL;
|
|
1730
|
+
typedef Vec<H, double> TV_ROW;
|
|
1731
|
+
enum { HEIGHT = H };
|
|
1732
|
+
enum { WIDTH = H };
|
|
1733
|
+
static constexpr bool IsLinear() { return false; }
|
|
1734
|
+
/// nothing to do
|
|
1735
|
+
Id () { ; }
|
|
1736
|
+
|
|
1737
|
+
INLINE auto View() const { return Id(); }
|
|
1738
|
+
INLINE constexpr auto Height () const { return H; }
|
|
1739
|
+
INLINE constexpr auto Width () const { return H; }
|
|
1740
|
+
INLINE tuple<size_t, size_t> Shape() const { return { H,H }; }
|
|
1741
|
+
///
|
|
1742
|
+
double operator() (int i) const
|
|
1743
|
+
{
|
|
1744
|
+
static_assert (true, "linear access of id");
|
|
1745
|
+
cerr << "id, linear access" << endl; return 0;
|
|
1746
|
+
}
|
|
1747
|
+
///
|
|
1748
|
+
double operator() (int i, int j) const { return (i == j) ? 1 : 0; }
|
|
1749
|
+
|
|
1750
|
+
};
|
|
1751
|
+
|
|
1752
|
+
|
|
1753
|
+
|
|
1754
|
+
|
|
1755
|
+
/// Variable size identity matrix
|
|
1756
|
+
class Identity : public MatExpr<Identity >
|
|
1757
|
+
{
|
|
1758
|
+
size_t size;
|
|
1759
|
+
public:
|
|
1760
|
+
typedef double TELEM;
|
|
1761
|
+
typedef double TSCAL;
|
|
1762
|
+
static constexpr bool IsLinear() { return false; }
|
|
1763
|
+
|
|
1764
|
+
INLINE Identity (size_t s) : size(s) { ; }
|
|
1765
|
+
|
|
1766
|
+
INLINE double operator() (int i) const
|
|
1767
|
+
{ cerr << "Identity, linear access" << endl; return 0; }
|
|
1768
|
+
|
|
1769
|
+
INLINE double operator() (int i, int j) const { return (i == j) ? 1 : 0; }
|
|
1770
|
+
INLINE auto View() const { return Identity(size); }
|
|
1771
|
+
INLINE tuple<size_t, size_t> Shape() const { return { size, size }; }
|
|
1772
|
+
INLINE auto Height () const { return size; }
|
|
1773
|
+
INLINE auto Width () const { return size; }
|
|
1774
|
+
};
|
|
1775
|
+
|
|
1776
|
+
|
|
1777
|
+
|
|
1778
|
+
|
|
1779
|
+
|
|
1780
|
+
template<int H, int W, typename T>
|
|
1781
|
+
inline std::ostream & operator<< (std::ostream & s, const Mat<H,W,T> & m)
|
|
1782
|
+
{
|
|
1783
|
+
for (int i = 0; i < H*W; i++)
|
|
1784
|
+
s << " " << setw(7) << m(i);
|
|
1785
|
+
return s;
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1788
|
+
|
|
1789
|
+
|
|
1790
|
+
template <int H, int W, typename T>
|
|
1791
|
+
INLINE auto Trans (const Mat<H,W,T> & mat)
|
|
1792
|
+
-> Mat<W,H,decltype(Trans(mat(0,0)))>
|
|
1793
|
+
{
|
|
1794
|
+
Mat<W,H,decltype(Trans(mat(0,0)))> res;
|
|
1795
|
+
/*
|
|
1796
|
+
for (int i = 0; i < H; i++)
|
|
1797
|
+
for (int j = 0; j < W; j++)
|
|
1798
|
+
res(j,i) = mat(i,j);
|
|
1799
|
+
*/
|
|
1800
|
+
Iterate<H> ([&] (auto i) {
|
|
1801
|
+
Iterate<W> ([&] (auto j) {
|
|
1802
|
+
res(j.value,i.value) = mat(i.value, j.value);
|
|
1803
|
+
});
|
|
1804
|
+
});
|
|
1805
|
+
return res;
|
|
1806
|
+
}
|
|
1807
|
+
|
|
1808
|
+
template <int H, int W, typename T>
|
|
1809
|
+
INLINE Mat<H,W,T> operator- (const Mat<H,W,T> & mat)
|
|
1810
|
+
{
|
|
1811
|
+
Mat<H,W,typename remove_const<T>::type> res;
|
|
1812
|
+
Iterate<H*W> ([&] (auto i) {
|
|
1813
|
+
res(i.value) = -mat(i.value);
|
|
1814
|
+
});
|
|
1815
|
+
return res;
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
|
|
1819
|
+
|
|
1820
|
+
template <int H, int W, typename T>
|
|
1821
|
+
INLINE Mat<H,W,T> operator+ (const Mat<H,W,T> & ma, const Mat<H,W,T> & mb)
|
|
1822
|
+
{
|
|
1823
|
+
Mat<H,W,typename remove_const<T>::type> res;
|
|
1824
|
+
Iterate<H*W> ([&] (auto i) {
|
|
1825
|
+
res(i.value) = ma(i.value) + mb(i.value);
|
|
1826
|
+
});
|
|
1827
|
+
return res;
|
|
1828
|
+
}
|
|
1829
|
+
|
|
1830
|
+
template <int H, int W, typename T>
|
|
1831
|
+
INLINE Mat<H,W,T> operator- (const Mat<H,W,T> & ma, const Mat<H,W,T> & mb)
|
|
1832
|
+
{
|
|
1833
|
+
Mat<H,W,typename remove_const<T>::type> res;
|
|
1834
|
+
Iterate<H*W> ([&] (auto i) {
|
|
1835
|
+
res(i.value) = ma(i.value) - mb(i.value);
|
|
1836
|
+
});
|
|
1837
|
+
return res;
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1840
|
+
template <int H, int W, typename T>
|
|
1841
|
+
INLINE Mat<H,W,T> & operator+= (Mat<H,W,T> & ma, const Mat<H,W,T> & mb)
|
|
1842
|
+
{
|
|
1843
|
+
Iterate<H*W> ([&] (auto i) {
|
|
1844
|
+
ma(i.value) += mb(i.value);
|
|
1845
|
+
});
|
|
1846
|
+
return ma;
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1849
|
+
template <int H, int W, typename T>
|
|
1850
|
+
INLINE Mat<H,W,T> & operator-= (Mat<H,W,T> & ma, const Mat<H,W,T> & mb)
|
|
1851
|
+
{
|
|
1852
|
+
Iterate<H*W> ([&] (auto i) {
|
|
1853
|
+
ma(i.value) -= mb(i.value);
|
|
1854
|
+
});
|
|
1855
|
+
return ma;
|
|
1856
|
+
}
|
|
1857
|
+
|
|
1858
|
+
|
|
1859
|
+
|
|
1860
|
+
template <int H, int W, typename T>
|
|
1861
|
+
INLINE Mat<H,W,T> operator* (T scal, const Mat<H,W,T> & mat)
|
|
1862
|
+
{
|
|
1863
|
+
Mat<H,W,typename remove_const<T>::type> res;
|
|
1864
|
+
Iterate<H*W> ([&] (auto i) {
|
|
1865
|
+
res(i.value) = scal * mat(i.value);
|
|
1866
|
+
});
|
|
1867
|
+
return res;
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
|
|
1871
|
+
template <int H, int W, int W2, typename T1, typename T2>
|
|
1872
|
+
INLINE auto operator* (const Mat<H,W,T1> & mat1, const Mat<W,W2,T2> & mat2)
|
|
1873
|
+
-> Mat<H,W2,decltype( RemoveConst(mat1(0,0)*mat2(0,0)))>
|
|
1874
|
+
{
|
|
1875
|
+
typedef decltype( RemoveConst(mat1(0,0)*mat2(0))) TRES;
|
|
1876
|
+
Mat<H,W2,TRES> res;
|
|
1877
|
+
for (int i = 0; i < H; i++)
|
|
1878
|
+
for (int j = 0; j < W2; j++)
|
|
1879
|
+
{
|
|
1880
|
+
TRES sum(0);
|
|
1881
|
+
for (int k = 0; k < W; k++)
|
|
1882
|
+
sum += mat1(i,k) * mat2(k,j);
|
|
1883
|
+
res(i,j) = sum;
|
|
1884
|
+
}
|
|
1885
|
+
return res;
|
|
1886
|
+
}
|
|
1887
|
+
|
|
1888
|
+
|
|
1889
|
+
template <int H, int W, int W2, typename T1, typename T2>
|
|
1890
|
+
INLINE auto operator* (const Mat<H,W,T1> & mat, const Vec<W2,T2> & vec)
|
|
1891
|
+
-> Vec<H, decltype(RemoveConst(mat(0,0)*vec(0)))>
|
|
1892
|
+
{
|
|
1893
|
+
static_assert(W == W2, "Mat * Vec dimension mismatch!");
|
|
1894
|
+
typedef decltype(RemoveConst(mat(0,0)*vec(0))) TRES;
|
|
1895
|
+
Vec<H, TRES> res = TRES(0);
|
|
1896
|
+
for (int i = 0; i < H; i++)
|
|
1897
|
+
for (int j = 0; j < W; j++)
|
|
1898
|
+
res(i) += mat(i,j) * vec(j);
|
|
1899
|
+
return res;
|
|
1900
|
+
}
|
|
1901
|
+
|
|
1902
|
+
template <int H, int W, int W2, typename T1, typename T2>
|
|
1903
|
+
INLINE auto operator* (const Mat<H,W,T1> & mat, const FlatVec<W2,T2> & vec)
|
|
1904
|
+
-> Vec<H, decltype(RemoveConst(mat(0,0)*vec(0)))>
|
|
1905
|
+
{
|
|
1906
|
+
static_assert(W == W2, "Mat * FlatVec dimension mismatch!");
|
|
1907
|
+
typedef decltype(RemoveConst(mat(0,0)*vec(0))) TRES;
|
|
1908
|
+
Vec<H, TRES> res = TRES(0);
|
|
1909
|
+
for (int i = 0; i < H; i++)
|
|
1910
|
+
for (int j = 0; j < W; j++)
|
|
1911
|
+
res(i) += mat(i,j) * vec(j);
|
|
1912
|
+
return res;
|
|
1913
|
+
}
|
|
1914
|
+
|
|
1915
|
+
template <int H, int W, typename T1, typename T2>
|
|
1916
|
+
INLINE auto operator* (const Mat<H,W,T1> & mat, FlatVector<T2> vec)
|
|
1917
|
+
-> Vec<H, decltype(RemoveConst(mat(0,0)*vec(0)))>
|
|
1918
|
+
{
|
|
1919
|
+
NETGEN_CHECK_RANGE(vec.Size(), W, W+1);
|
|
1920
|
+
typedef decltype(RemoveConst(mat(0,0)*vec(0))) TRES;
|
|
1921
|
+
Vec<H, TRES> res = TRES(0);
|
|
1922
|
+
for (int i = 0; i < H; i++)
|
|
1923
|
+
for (int j = 0; j < W; j++)
|
|
1924
|
+
res(i) += mat(i,j) * vec(j);
|
|
1925
|
+
return res;
|
|
1926
|
+
}
|
|
1927
|
+
|
|
1928
|
+
template <typename T>
|
|
1929
|
+
auto Det (Mat<0,0,T> m)
|
|
1930
|
+
{
|
|
1931
|
+
return T(1.0);
|
|
1932
|
+
}
|
|
1933
|
+
|
|
1934
|
+
|
|
1935
|
+
template <typename T>
|
|
1936
|
+
auto Det (Mat<1,1,T> m)
|
|
1937
|
+
{
|
|
1938
|
+
return m(0,0);
|
|
1939
|
+
}
|
|
1940
|
+
|
|
1941
|
+
template <typename T>
|
|
1942
|
+
auto Det (Mat<2,2,T> m)
|
|
1943
|
+
{
|
|
1944
|
+
return m(0,0)*m(1,1) - m(0,1)*m(1,0);
|
|
1945
|
+
}
|
|
1946
|
+
|
|
1947
|
+
template <typename T>
|
|
1948
|
+
auto Det (Mat<3,3,T> m)
|
|
1949
|
+
{
|
|
1950
|
+
return
|
|
1951
|
+
m(0) * (m(4) * m(8) - m(5) * m(7)) +
|
|
1952
|
+
m(1) * (m(5) * m(6) - m(3) * m(8)) +
|
|
1953
|
+
m(2) * (m(3) * m(7) - m(4) * m(6));
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
|
|
1957
|
+
|
|
1958
|
+
|
|
1959
|
+
|
|
1960
|
+
template <int H, int W, typename T>
|
|
1961
|
+
INLINE Mat<H,W,T> Adj (Mat<H,W,T> m)
|
|
1962
|
+
{
|
|
1963
|
+
cerr << "Adj<" << H << "," << W << "> not implemented" << endl;
|
|
1964
|
+
return m;
|
|
1965
|
+
}
|
|
1966
|
+
|
|
1967
|
+
|
|
1968
|
+
template <typename T>
|
|
1969
|
+
INLINE Mat<0,0,T> Adj (Mat<0,0,T> m)
|
|
1970
|
+
{
|
|
1971
|
+
return Mat<0,0,T>();
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1974
|
+
template <typename T>
|
|
1975
|
+
INLINE Mat<1,1,T> Adj (Mat<1,1,T> m)
|
|
1976
|
+
{
|
|
1977
|
+
Mat<1,1,T> adj;
|
|
1978
|
+
adj(0,0) = T(1); // m(0,0);
|
|
1979
|
+
return adj;
|
|
1980
|
+
}
|
|
1981
|
+
|
|
1982
|
+
template <typename T>
|
|
1983
|
+
INLINE Mat<2,2,T> Adj (Mat<2,2,T> m)
|
|
1984
|
+
{
|
|
1985
|
+
Mat<2,2,T> adj;
|
|
1986
|
+
adj(0,0) = m(1,1);
|
|
1987
|
+
adj(0,1) = -m(0,1);
|
|
1988
|
+
adj(1,0) = -m(1,0);
|
|
1989
|
+
adj(1,1) = m(0,0);
|
|
1990
|
+
return adj;
|
|
1991
|
+
}
|
|
1992
|
+
|
|
1993
|
+
|
|
1994
|
+
template <typename T>
|
|
1995
|
+
INLINE Mat<3,3,T> Adj (Mat<3,3,T> m)
|
|
1996
|
+
{
|
|
1997
|
+
Mat<3,3,T> adj;
|
|
1998
|
+
adj(0,0) = m(4)*m(8)-m(5)*m(7);
|
|
1999
|
+
adj(0,1) = -(m(1) * m(8) - m(2) * m(7));
|
|
2000
|
+
adj(0,2) = m(1) * m(5) - m(2) * m(4);
|
|
2001
|
+
|
|
2002
|
+
adj(1,0) = m(5)*m(6)-m(3)*m(8);
|
|
2003
|
+
adj(1,1) = m(0) * m(8) - m(2) * m(6);
|
|
2004
|
+
adj(1,2) = -(m(0) * m(5) - m(2) * m(3));
|
|
2005
|
+
|
|
2006
|
+
adj(2,0) = (m(3)*m(7)-m(4)*m(6));
|
|
2007
|
+
adj(2,1) = -(m(0) * m(7) - m(1) * m(6));
|
|
2008
|
+
adj(2,2) = (m(0) * m(4) - m(1) * m(3));
|
|
2009
|
+
return adj;
|
|
2010
|
+
}
|
|
2011
|
+
|
|
2012
|
+
|
|
2013
|
+
|
|
2014
|
+
template <int H, int W, typename T>
|
|
2015
|
+
INLINE Mat<H,W,T> Cof (Mat<H,W,T> m)
|
|
2016
|
+
{
|
|
2017
|
+
cerr << "Cof<" << H << "," << W << "> not implemented" << endl;
|
|
2018
|
+
return m;
|
|
2019
|
+
}
|
|
2020
|
+
|
|
2021
|
+
template <typename T>
|
|
2022
|
+
INLINE Mat<0,0,T> Cof (Mat<0,0,T> m)
|
|
2023
|
+
{
|
|
2024
|
+
return Mat<0,0,T>();
|
|
2025
|
+
}
|
|
2026
|
+
|
|
2027
|
+
template <typename T>
|
|
2028
|
+
INLINE Mat<1,1,T> Cof (Mat<1,1,T> m)
|
|
2029
|
+
{
|
|
2030
|
+
Mat<1,1,T> cof;
|
|
2031
|
+
cof(0,0) = T(1); // m(0,0);
|
|
2032
|
+
return cof;
|
|
2033
|
+
}
|
|
2034
|
+
|
|
2035
|
+
template <typename T>
|
|
2036
|
+
INLINE Mat<2,2,T> Cof (Mat<2,2,T> m)
|
|
2037
|
+
{
|
|
2038
|
+
Mat<2,2,T> cof;
|
|
2039
|
+
cof(0,0) = m(1,1);
|
|
2040
|
+
cof(0,1) = -m(1,0);
|
|
2041
|
+
cof(1,0) = -m(0,1);
|
|
2042
|
+
cof(1,1) = m(0,0);
|
|
2043
|
+
return cof;
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2046
|
+
|
|
2047
|
+
template <typename T>
|
|
2048
|
+
INLINE Mat<3,3,T> Cof (Mat<3,3,T> m)
|
|
2049
|
+
{
|
|
2050
|
+
Mat<3,3,T> cof;
|
|
2051
|
+
cof(0,0) = m(1,1)*m(2,2)-m(2,1)*m(1,2);
|
|
2052
|
+
cof(0,1) = -m(1,0)*m(2,2)+m(2,0)*m(1,2);
|
|
2053
|
+
cof(0,2) = m(1,0)*m(2,1)-m(2,0)*m(1,1);
|
|
2054
|
+
|
|
2055
|
+
cof(1,0) = -m(0,1)*m(2,2)+m(2,1)*m(0,2);
|
|
2056
|
+
cof(1,1) = m(0,0)*m(2,2)-m(2,0)*m(0,2);
|
|
2057
|
+
cof(1,2) = -m(0,0)*m(2,1)+m(2,0)*m(0,1);
|
|
2058
|
+
|
|
2059
|
+
cof(2,0) = m(0,1)*m(1,2)-m(1,1)*m(0,2);
|
|
2060
|
+
cof(2,1) = -m(0,0)*m(1,2)+m(1,0)*m(0,2);
|
|
2061
|
+
cof(2,2) = m(0,0)*m(1,1)-m(1,0)*m(0,1);
|
|
2062
|
+
return cof;
|
|
2063
|
+
}
|
|
2064
|
+
|
|
2065
|
+
|
|
2066
|
+
template <typename T>
|
|
2067
|
+
INLINE Mat<4,4,T> Cof (Mat<4,4,T> m)
|
|
2068
|
+
{
|
|
2069
|
+
Mat<4,4,T> cof;
|
|
2070
|
+
cof(0,0) = (m(1,1)*m(2,2)*m(3,3)+m(1,2)*m(2,3)*m(3,1)+m(1,3)*m(2,1)*m(3,2) - m(1,1)*m(3,2)*m(2,3) - m(2,1)*m(1,2)*m(3,3) - m(3,1)*m(2,2)*m(1,3));
|
|
2071
|
+
cof(0,1) = -(m(1,0)*m(2,2)*m(3,3)+m(1,2)*m(2,3)*m(3,0)+m(1,3)*m(2,0)*m(3,2) - m(1,0)*m(3,2)*m(2,3) - m(2,0)*m(1,2)*m(3,3) - m(3,0)*m(2,2)*m(1,3));
|
|
2072
|
+
cof(0,2) = (m(1,0)*m(2,1)*m(3,3)+m(1,1)*m(2,3)*m(3,0)+m(1,3)*m(2,0)*m(3,1) - m(1,0)*m(3,1)*m(2,3) - m(2,0)*m(1,1)*m(3,3) - m(3,0)*m(2,1)*m(1,3));
|
|
2073
|
+
cof(0,3) = -(m(1,0)*m(2,1)*m(3,2)+m(1,1)*m(2,2)*m(3,0)+m(1,2)*m(2,0)*m(3,1) - m(1,0)*m(3,1)*m(2,2) - m(2,0)*m(1,1)*m(3,2) - m(3,0)*m(2,1)*m(1,2));
|
|
2074
|
+
|
|
2075
|
+
cof(1,0) = -(m(0,1)*m(2,2)*m(3,3)+m(0,2)*m(2,3)*m(3,1)+m(0,3)*m(2,1)*m(3,2) - m(0,1)*m(3,2)*m(2,3) - m(2,1)*m(0,2)*m(3,3) - m(3,1)*m(2,2)*m(0,3));
|
|
2076
|
+
cof(1,1) = (m(0,0)*m(2,2)*m(3,3)+m(0,2)*m(2,3)*m(3,0)+m(0,3)*m(2,0)*m(3,2) - m(0,0)*m(3,2)*m(2,3) - m(2,0)*m(0,2)*m(3,3) - m(3,0)*m(2,2)*m(0,3));
|
|
2077
|
+
cof(1,2) = -(m(0,0)*m(2,1)*m(3,3)+m(0,1)*m(2,3)*m(3,0)+m(0,3)*m(2,0)*m(3,1) - m(0,0)*m(3,1)*m(2,3) - m(2,0)*m(0,1)*m(3,3) - m(3,0)*m(2,1)*m(0,3));
|
|
2078
|
+
cof(1,3) = (m(0,0)*m(2,1)*m(3,2)+m(0,1)*m(2,2)*m(3,0)+m(0,2)*m(2,0)*m(3,1) - m(0,0)*m(3,1)*m(2,2) - m(2,0)*m(0,1)*m(3,2) - m(3,0)*m(2,1)*m(0,2));
|
|
2079
|
+
|
|
2080
|
+
cof(2,0) = (m(0,1)*m(1,2)*m(3,3)+m(0,2)*m(1,3)*m(3,1)+m(0,3)*m(1,1)*m(3,2) - m(0,1)*m(3,2)*m(1,3) - m(1,1)*m(0,2)*m(3,3) - m(3,1)*m(1,2)*m(0,3));
|
|
2081
|
+
cof(2,1) = -(m(0,0)*m(1,2)*m(3,3)+m(0,2)*m(1,3)*m(3,0)+m(0,3)*m(1,0)*m(3,2) - m(0,0)*m(3,2)*m(1,3) - m(1,0)*m(0,2)*m(3,3) - m(3,0)*m(1,2)*m(0,3));
|
|
2082
|
+
cof(2,2) = (m(0,0)*m(1,1)*m(3,3)+m(0,1)*m(1,3)*m(3,0)+m(0,3)*m(1,0)*m(3,1) - m(0,0)*m(3,1)*m(1,3) - m(1,0)*m(0,1)*m(3,3) - m(3,0)*m(1,1)*m(0,3));
|
|
2083
|
+
cof(2,3) = -(m(0,0)*m(1,1)*m(3,2)+m(0,1)*m(1,2)*m(3,0)+m(0,2)*m(1,0)*m(3,1) - m(0,0)*m(3,1)*m(1,2) - m(1,0)*m(0,1)*m(3,2) - m(3,0)*m(1,1)*m(0,2));
|
|
2084
|
+
|
|
2085
|
+
cof(3,0) = -(m(0,1)*m(1,2)*m(2,3)+m(0,2)*m(1,3)*m(2,1)+m(0,3)*m(1,1)*m(2,2) - m(0,1)*m(2,2)*m(1,3) - m(1,1)*m(0,2)*m(2,3) - m(2,1)*m(1,2)*m(0,3));
|
|
2086
|
+
cof(3,1) = (m(0,0)*m(1,2)*m(2,3)+m(0,2)*m(1,3)*m(2,0)+m(0,3)*m(1,0)*m(2,2) - m(0,0)*m(2,2)*m(1,3) - m(1,0)*m(0,2)*m(2,3) - m(2,0)*m(1,2)*m(0,3));
|
|
2087
|
+
cof(3,2) = -(m(0,0)*m(1,1)*m(2,3)+m(0,1)*m(1,3)*m(2,0)+m(0,3)*m(1,0)*m(2,1) - m(0,0)*m(2,1)*m(1,3) - m(1,0)*m(0,1)*m(2,3) - m(2,0)*m(1,1)*m(0,3));
|
|
2088
|
+
cof(3,3) = (m(0,0)*m(1,1)*m(2,2)+m(0,1)*m(1,2)*m(2,0)+m(0,2)*m(1,0)*m(2,1) - m(0,0)*m(2,1)*m(1,2) - m(1,0)*m(0,1)*m(2,2) - m(2,0)*m(1,1)*m(0,2));
|
|
2089
|
+
return cof;
|
|
2090
|
+
}
|
|
2091
|
+
|
|
2092
|
+
|
|
2093
|
+
template <int H, int W, typename T>
|
|
2094
|
+
INLINE Mat<H,W,T> Inv (Mat<H,W,T> m)
|
|
2095
|
+
{
|
|
2096
|
+
return 1.0/Det(m) * Adj(m);
|
|
2097
|
+
}
|
|
2098
|
+
|
|
2099
|
+
|
|
2100
|
+
|
|
2101
|
+
|
|
2102
|
+
template <int H, int W, typename SCAL, typename TANY>
|
|
2103
|
+
inline void AtomicAdd (Mat<H,W,SCAL> & x, TANY y)
|
|
2104
|
+
{
|
|
2105
|
+
for (int i = 0; i < H; i++)
|
|
2106
|
+
for (int j = 0; j < W; j++)
|
|
2107
|
+
AtomicAdd (x(i,j), y(i,j));
|
|
2108
|
+
}
|
|
2109
|
+
|
|
2110
|
+
}
|
|
2111
|
+
|
|
2112
|
+
|
|
2113
|
+
namespace ngstd
|
|
2114
|
+
{
|
|
2115
|
+
template <typename ARCHIVE, int N, int M, typename T>
|
|
2116
|
+
inline auto & operator& (ARCHIVE & ar, ngbla::Mat<N,M,T> & m)
|
|
2117
|
+
{
|
|
2118
|
+
for (int i = 0; i < N*M; i++)
|
|
2119
|
+
ar & m(i);
|
|
2120
|
+
return ar;
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
|
|
2124
|
+
|
|
2125
|
+
|
|
2126
|
+
namespace ngcore
|
|
2127
|
+
{
|
|
2128
|
+
template<typename T>
|
|
2129
|
+
struct MPI_typetrait;
|
|
2130
|
+
|
|
2131
|
+
template<int N, int M, typename T>
|
|
2132
|
+
struct MPI_typetrait<ngbla::Mat<N,M,T> > {
|
|
2133
|
+
static auto MPIType () {
|
|
2134
|
+
return MPI_typetrait<std::array<T,N*M>>::MPIType();
|
|
2135
|
+
}
|
|
2136
|
+
};
|
|
2137
|
+
}
|
|
2138
|
+
|
|
2139
|
+
|
|
2140
|
+
#endif
|