|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Comprehensive example demonstrating sparse matrices and vectors in Aleph-w. More...
#include <iostream>#include <iomanip>#include <string>#include <memory>#include <tclap/CmdLine.h>#include <al-vector.H>#include <al-matrix.H>#include <al-domain.H>#include <htlist.H>#include <ahFunctional.H>Go to the source code of this file.
Functions | |
| void | print_section (const string &title) |
| void | print_subsection (const string &title) |
| void | demo_sparse_vector () |
| void | demo_string_indexed_vector () |
| void | demo_sparse_matrix () |
| void | demo_named_matrix () |
| void | demo_vector_arithmetic () |
| void | demo_adjacency_matrix () |
| void | demo_epsilon () |
| void | demo_initializer_list () |
| void | demo_transpose () |
| void | demo_identity () |
| void | demo_matrix_vector_mult () |
| void | demo_matrix_mult () |
| void | demo_outer_product () |
| void | demo_comparison () |
| void | demo_matrix_arithmetic () |
| void | demo_row_col_operations () |
| void | demo_linear_system () |
| int | main (int argc, char *argv[]) |
Comprehensive example demonstrating sparse matrices and vectors in Aleph-w.
This program demonstrates sparse Matrix and Vector classes from al-matrix.H and al-vector.H. Unlike dense matrices (which store all elements), sparse matrices only store non-zero entries, making them memory-efficient for sparse data. Additionally, they support domain-based indexing, allowing rows and columns to be indexed by any type (not just integers).
Use sparse when:
Example: 1000×1000 matrix with only 1000 non-zeros:
Unlike traditional matrices (indexed 0..n-1), Aleph-w matrices support:
matrix["row_name"]["col_name"]Example:
get_entry(), set_entry()+, -, +=, -= for matrices/vectorsmult_by_scalar(), scalar * matrixget_row_vector(), get_col_vector()set_vector_as_row(), set_vector_as_col()transpose()identity() (for square matrices)vector_matrix_mult(), matrix_vector_mult()operator*, mult_matrix_vector_sparse(), mult_matrix_vector_dot_product(), mult_matrix_vector_linear_comb()operator*, mult_vector_matrix_linear_comb()outer_product(v1, v2)==, !=, equal_to() (with epsilon tolerance)to_rowlist(), to_collist(), to_str()| Operation | Dense | Sparse | Notes |
|---|---|---|---|
| Storage | O(n²) | O(nnz) | Sparse wins for sparse data |
| Access | O(1) | O(1) avg | Hash-based storage |
| Addition | O(n²) | O(nnz) | Sparse much faster |
| M×v mult | O(n²) | O(nnz) | Sparse iteration available |
| M×M mult | O(n³) | O(nnz₁×nnz₂) | Depends on sparsity |
Definition in file matrix_example.C.
| void demo_adjacency_matrix | ( | ) |
Definition at line 447 of file matrix_example.C.
References Aleph::Matrix< Trow, Tcol, NumType >::get_entry(), LocateFunctions< Container, Type >::get_it(), Aleph::Matrix< Trow, Tcol, NumType >::get_row_vector(), Aleph::DynList< T >::insert(), Aleph::maps(), nodes, num_nodes, print_section(), print_subsection(), and Aleph::Matrix< Trow, Tcol, NumType >::set_entry().
Referenced by main().
| void demo_comparison | ( | ) |
Definition at line 851 of file matrix_example.C.
References Aleph::DynList< T >::insert(), Aleph::maps(), print_section(), and print_subsection().
Referenced by main().
| void demo_epsilon | ( | ) |
Definition at line 511 of file matrix_example.C.
References Aleph::Vector< T, NumType >::get_epsilon(), Aleph::Vector< T, NumType >::get_it(), Aleph::DynList< T >::insert(), Aleph::maps(), print_section(), print_subsection(), Aleph::Vector< T, NumType >::set_entry(), and Aleph::Vector< T, NumType >::set_epsilon().
Referenced by main().
| void demo_identity | ( | ) |
Definition at line 638 of file matrix_example.C.
References Aleph::DynList< T >::insert(), Aleph::maps(), and print_section().
Referenced by main().
| void demo_initializer_list | ( | ) |
Definition at line 560 of file matrix_example.C.
References Aleph::DynList< T >::insert(), Aleph::maps(), print_section(), and print_subsection().
Referenced by main().
| void demo_linear_system | ( | ) |
Definition at line 1029 of file matrix_example.C.
References Aleph::DynList< T >::insert(), Aleph::maps(), print_section(), Aleph::Vector< T, NumType >::set_entry(), Aleph::Matrix< Trow, Tcol, NumType >::set_entry(), and Aleph::Matrix< Trow, Tcol, NumType >::to_str().
Referenced by main().
| void demo_matrix_arithmetic | ( | ) |
Definition at line 906 of file matrix_example.C.
References Aleph::diff(), Aleph::DynList< T >::insert(), Aleph::maps(), Aleph::Matrix< Trow, Tcol, NumType >::mult_by_scalar(), print_section(), print_subsection(), Aleph::sum(), and Aleph::Matrix< Trow, Tcol, NumType >::to_str().
Referenced by main().
| void demo_matrix_mult | ( | ) |
Definition at line 752 of file matrix_example.C.
References Aleph::DynList< T >::insert(), Aleph::maps(), and print_section().
Referenced by main().
| void demo_matrix_vector_mult | ( | ) |
Definition at line 670 of file matrix_example.C.
References LocateFunctions< Container, Type >::get_it(), Aleph::DynList< T >::insert(), Aleph::maps(), print_section(), print_subsection(), Aleph::Vector< T, NumType >::set_entry(), and Aleph::Matrix< Trow, Tcol, NumType >::set_entry().
Referenced by main().
| void demo_named_matrix | ( | ) |
Definition at line 322 of file matrix_example.C.
References LocateFunctions< Container, Type >::get_it(), Aleph::DynList< T >::insert(), Aleph::maps(), print_section(), and print_subsection().
Referenced by main().
| void demo_outer_product | ( | ) |
Definition at line 810 of file matrix_example.C.
References Aleph::DynList< T >::insert(), Aleph::maps(), Aleph::outer_product(), print_section(), and Aleph::Vector< T, NumType >::set_entry().
Referenced by main().
| void demo_row_col_operations | ( | ) |
Definition at line 963 of file matrix_example.C.
References Aleph::Matrix< Trow, Tcol, NumType >::get_col_vector(), LocateFunctions< Container, Type >::get_it(), Aleph::Matrix< Trow, Tcol, NumType >::get_row_vector(), Aleph::DynList< T >::insert(), Aleph::maps(), print_section(), print_subsection(), Aleph::Matrix< Trow, Tcol, NumType >::set_entry(), Aleph::Matrix< Trow, Tcol, NumType >::set_vector_as_row(), Aleph::HTList::size(), Aleph::Matrix< Trow, Tcol, NumType >::to_rowlist(), and Aleph::Matrix< Trow, Tcol, NumType >::to_str().
Referenced by main().
| void demo_sparse_matrix | ( | ) |
Definition at line 275 of file matrix_example.C.
References Aleph::Matrix< Trow, Tcol, NumType >::get_entry(), Aleph::DynList< T >::insert(), Aleph::maps(), print_section(), print_subsection(), and Aleph::Matrix< Trow, Tcol, NumType >::set_entry().
Referenced by main().
| void demo_sparse_vector | ( | ) |
Definition at line 177 of file matrix_example.C.
References Aleph::Vector< T, NumType >::get_entry(), Aleph::Vector< T, NumType >::get_it(), Aleph::DynList< T >::insert(), Aleph::maps(), print_section(), print_subsection(), and Aleph::Vector< T, NumType >::set_entry().
Referenced by main().
| void demo_string_indexed_vector | ( | ) |
Definition at line 227 of file matrix_example.C.
References Aleph::Vector< T, NumType >::get_entry(), LocateFunctions< Container, Type >::get_it(), Aleph::Vector< T, NumType >::get_it(), Aleph::DynList< T >::insert(), Aleph::maps(), print_section(), print_subsection(), and Aleph::Vector< T, NumType >::set_entry().
Referenced by main().
| void demo_transpose | ( | ) |
Definition at line 603 of file matrix_example.C.
References Aleph::Matrix< Trow, Tcol, NumType >::get_entry(), Aleph::DynList< T >::insert(), Aleph::maps(), print_section(), Aleph::Matrix< Trow, Tcol, NumType >::set_entry(), Aleph::Matrix< Trow, Tcol, NumType >::to_str(), and Aleph::Matrix< Trow, Tcol, NumType >::transpose().
Referenced by main().
| void demo_vector_arithmetic | ( | ) |
Definition at line 393 of file matrix_example.C.
References Aleph::diff(), Aleph::DynList< T >::insert(), Aleph::maps(), print_section(), print_subsection(), print_vec(), Aleph::Vector< T, NumType >::set_entry(), and Aleph::sum().
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 1076 of file matrix_example.C.
References demo_adjacency_matrix(), demo_comparison(), demo_epsilon(), demo_identity(), demo_initializer_list(), demo_linear_system(), demo_matrix_arithmetic(), demo_matrix_mult(), demo_matrix_vector_mult(), demo_named_matrix(), demo_outer_product(), demo_row_col_operations(), demo_sparse_matrix(), demo_sparse_vector(), demo_string_indexed_vector(), demo_transpose(), demo_vector_arithmetic(), and Aleph::maps().
| void print_section | ( | const string & | title | ) |
Definition at line 161 of file matrix_example.C.
References Aleph::maps().
Referenced by demo_adjacency_matrix(), demo_comparison(), demo_epsilon(), demo_identity(), demo_initializer_list(), demo_linear_system(), demo_matrix_arithmetic(), demo_matrix_mult(), demo_matrix_vector_mult(), demo_named_matrix(), demo_outer_product(), demo_row_col_operations(), demo_sparse_matrix(), demo_sparse_vector(), demo_string_indexed_vector(), demo_transpose(), and demo_vector_arithmetic().
| void print_subsection | ( | const string & | title | ) |
Definition at line 168 of file matrix_example.C.
References Aleph::maps().
Referenced by demo_adjacency_matrix(), demo_comparison(), demo_epsilon(), demo_initializer_list(), demo_matrix_arithmetic(), demo_matrix_vector_mult(), demo_named_matrix(), demo_row_col_operations(), demo_sparse_matrix(), demo_sparse_vector(), demo_string_indexed_vector(), and demo_vector_arithmetic().