Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
al-matrix.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
37# include <gtest/gtest.h>
38
39# include <ahSort.H>
40# include <ah-string-utils.H>
41# include <al-matrix.H>
42
43using namespace std;
44using namespace testing;
45using namespace Aleph;
46
47struct SmallDomains : testing::Test
48{
49 AlDomain<char> rd = { 'a', 'b', 'c', 'd', 'e' };
50 AlDomain<string> cd = { "A", "B", "C", "D" };
55
56 V1 v1_zero = { rd };
57 V1 v1 = { rd, range<int>(rd.size()) };
58 V1 v1_odd = { rd, range<int>(rd.size()).
59 maps([] (auto i) { return (i % 2) == 0 ? 1 : 0; }) };
60 V1 v1_even = { rd, range<int>(rd.size()).
61 maps([] (auto i) { return (i % 2) == 0 ? 0 : 1; }) };
62
63 V2 v2_zero = { cd };
64 V2 v2 = { cd, range<int>(cd.size()) };
65 V2 v2_odd = { cd, range<int>(cd.size()).
66 maps([] (auto i) { return (i % 2) == 0 ? 1 : 0; }) };
67 V2 v2_even = { cd, range<int>(cd.size()).
68 maps([] (auto i) { return (i % 2) == 0 ? 0 : 1; }) };
69
70 Mat1 m1_zero = { rd, cd };
71 Mat1 m1_one = { rd, cd, rd.maps<DynList<int>>([&] (auto)
72 { return range<int>(cd.size()); }) };
75
76 // A row of m2 has a value per column, an element of rd, and a matrix by
77 // columns a vector per column: with fewer, the constructor and
78 // create_by_columns() throw range_error
79 Mat2 m2_zero = { cd, rd };
80 Mat2 m2_one = { cd, rd, cd.maps<DynList<int>>([&] (auto)
81 { return range<int>(rd.size()); }) };
84};
85
87{
88 EXPECT_EQ(m1_one.get_row_vector('a'), v2);
89 EXPECT_EQ(m1_one.get_row_vector('b'), v2);
90 EXPECT_EQ(m1_one.get_row_vector('c'), v2);
91 EXPECT_EQ(m1_one.get_row_vector('d'), v2);
92 EXPECT_EQ(m1_one.get_row_vector('e'), v2);
93
94 EXPECT_EQ(m1_one.get_col_vector("A"), V1(rd));
95 EXPECT_EQ(m1_one.get_col_vector("B"), V1(rd, rep(rd.size(), 1)));
96 EXPECT_EQ(m1_one.get_col_vector("C"), V1(rd, rep(rd.size(), 2)));
97 EXPECT_EQ(m1_one.get_col_vector("D"), V1(rd, rep(rd.size(), 3)));
98
99 EXPECT_EQ(v1*m1_zero, V2(cd, { 0, 0, 0, 0}));
100 EXPECT_EQ(m1_zero*v2, V1(rd, { 0, 0, 0, 0, 0}));
101
102 //cout << m1_even.matrix_vector_mult(m2_even) << endl << endl;
103
104 return;
105 cout << v1_zero << endl << endl
106 << v1 << endl << endl
107 << v1_odd << endl << endl
108 << v1_even << endl << endl
109 << endl
110 << v2_zero << endl << endl
111 << v2 << endl << endl
112 << v2_odd << endl << endl
113 << v2_even << endl << endl
114 << m1_even<< endl << endl
115 << m1_odd << endl << endl
116 << m2_even<< endl << endl
117 << m1_odd << endl << endl
118 << endl;
119}
120
122{
123 // using M1 = Matrix<char, char, int>;
124 // using M2 = Matrix<string, string, int>;
125 // cout << M1(rd, rd).identity() << endl
126 // << M2(cd, cd).identity() << endl;
127}
String manipulation utilities.
High-level sorting functions for Aleph containers.
Sparse matrix with generic domains.
TEST_F(SmallDomains, basic)
Definition al-matrix.cc:86
Generic domain class based on hash set.
Definition al-domain.H:132
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
Vector< Tcol, NumType > get_row_vector(const Trow &row) const
Given a row, build a vector corresponding to the row.
Definition al-matrix.H:718
static Matrix create_by_rows(const RDomain &rdomain, const DynList< Vector< string, int > > &rows, const int &e=default_epsilon)
Create a matrix from a list of row vectors.
Definition al-matrix.H:504
static Matrix create_by_columns(const CDomain &cdomain, const DynList< Vector< string, int > > &cols, const int &e=default_epsilon)
Create a matrix from a list of column vectors.
Definition al-matrix.H:535
Vector< Trow, NumType > get_col_vector(const Tcol &col) const
Given a column, build a vector corresponding to the column.
Definition al-matrix.H:733
Main namespace for Aleph-w library functions.
DynList< T > rep(const size_t n, const T &item)
Create a sequence of repeated items.
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
AlDomain< char > rd
Definition al-matrix.cc:49
AlDomain< string > cd
Definition al-matrix.cc:50