51# include <type_traits>
77 concept AlNumber = std::copyable<N>
and std::convertible_to<int, N>
78 and std::convertible_to<double, N>
79 and requires (
const N & a,
const N & b)
81 { -a } -> std::convertible_to<N>;
82 { a < 0 } -> std::convertible_to<bool>;
83 { a < b } -> std::convertible_to<bool>;
84 { a <= b } -> std::convertible_to<bool>;
85 { a >= b } -> std::convertible_to<bool>;
96 { a + b } -> std::convertible_to<N>;
97 { a - b } -> std::convertible_to<N>;
107 template <
typename N>
110 { a * b } -> std::convertible_to<N>;
112 { a == 1 } -> std::convertible_to<bool>;
120 template <
typename N>
124 { a == 1 } -> std::convertible_to<bool>;
127 template <
typename Trow,
typename Tcol,
typename NumType>
146 template <
typename T =
int,
typename NumType =
double>
150 template <
typename T,
typename NumType>
153 public LocateMixin<Vector<T, NumType>, std::pair<T, NumType>>,
169 static NumType
abs(
const NumType & val)
noexcept
171 return val < 0 ? -val : val;
174 bool is_zero(
const NumType & val)
const noexcept
180 static bool test_epsilon(
const NumType & e)
noexcept {
return e >= 0; }
199 using Pair = std::pair<T, NumType>;
253 template <
typename I>
requires std::is_integral_v<I>
272 entries(std::forward<Map>(v.entries))
290 <<
"Vector(DynList): list sizes does not match";
294 auto t = it.get_curr();
324 :
Vector(std::make_shared<Domain>(std::move(d)),
l, zero)
330 template <
typename I>
requires std::is_integral_v<I>
346 <<
"Vector assignment: domains must be the same";
367 <<
"Vector assignment: domains must be the same";
414 else if (ptr !=
nullptr)
428 std::initializer_list<NumType> lr)
431 <<
"size mismatch between domain and range";
433 auto itd = ld.begin();
434 auto itr = lr.begin();
435 for (; itd != ld.end(); ++itd, ++itr)
438 <<
"An item of first list doesn't belong to domain";
455 <<
"size mismatch between domain and range";
457 auto itr = lr.begin();
458 c.for_each([
this, &itr](
const T & key)
461 <<
"Key does not belong to domain";
542 bool are_equal(
const NumType & n1,
const NumType & n2)
const noexcept
560 return are_equal(other.get_entry(p.first), p.second);
562 other.entries.all([
this](
const Pair & p)
587 v.entries.for_each([
this](
const Pair & p)
599 v.entries.for_each([
this](
const Pair & p)
637 const_cast<Pair &
>(p).second *= scalar;
662 const_cast<Pair &
>(p).second /= scalar;
687 <<
"scalar_product: different domains";
692 if (
entries.size() < v.entries.size())
694 template foldl<NumType>(0, [&v](
const NumType & acc,
const Pair & p) -> NumType
696 return acc + p.second * v.get_entry(p.first);
700 template foldl<NumType>(0, [
this](
const NumType & acc,
const Pair & p) -> NumType
702 return acc +
get_entry(p.first) * p.second;
721 return domain_ptr->keys().template maps<NumType>([
this](
const T & i)
734 std::cout <<
"(" <<
k <<
"," <<
entries(
k) <<
") ";
742 requires (const NumType & n) { { std::to_string(n) } -> std::convertible_to<std::string>; }
748 std::ostringstream s;
760 typedef std::pair<std::string, std::string>
Pair;
764 template maps<Pair>([](
const Pair & p)
766 const size_t fsz = p.first.size();
767 const size_t ssz = p.second.size();
769 return Pair(
" " + p.first,
std::string(fsz - ssz + 1,
' ') + p.second);
770 return Pair(
std::string(ssz - fsz + 1,
' ') + p.first,
" " + p.second);
814 const NumType
value = proxy;
834 operator NumType() const noexcept
836 const NumType *ptr = std::as_const(*v_ptr).search_entry(
key);
837 return ptr ==
nullptr ? NumType(0) : *ptr;
894 template <
typename T,
typename NumType>
895 requires AlDomainKey<T>
and AlNumber<NumType>
900 template <
typename T,
typename NumType>
901 requires AlMultiplicativeNumber<NumType>
911 template <
typename T,
typename NumType>
912 requires AlOrderedDomainKey<T>
and AlStreamable<T>
and
913 requires (
const NumType & n) { { std::to_string(n) } -> std::convertible_to<std::string>; }
CRTP Mixins for container functionality (DRY principle).
Exception handling system with formatted messages for Aleph-w.
#define ah_length_error_if(C)
Throws std::length_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Zip iterators and functional operations for multiple containers.
DRY (Don't Repeat Yourself) utilities and macros.
Functional programming utilities for Aleph-w containers.
High-level sorting functions for Aleph containers.
Integer domain classes for sparse data structures.
size_t size_t int32_t value
Generic domain class based on hash set.
Read-only view of an Aleph iterator: the iterator of a constant container.
Doubly-linked list (defined in tpl_dynList.H).
size_t size() const noexcept
Return the number of items of the list, in constant time.
CRTP Mixin providing functional programming operations.
and
Transform elements using a mapping function.
CRTP Mixin providing element location operations.
Sparse matrix with generic row and column domains.
Number Theoretic Transform over Z / MOD Z.
CRTP Mixin providing traversal operations.
The entry of a key, as v[k] gives it: reads and assigns it.
Proxy & operator=(const Proxy &proxy)
Assign the value of the entry of proxy, zero included.
Proxy(Vector &v, const T &k)
The entry of k in v.
Proxy(const Proxy &)=default
Another proxy of the same entry.
Sparse vector implementation with domain-based indexing.
Vector(DomainPtr d, const NumType &zero=default_epsilon)
Construct a vector over a given domain (shared_ptr version)
bool operator==(const Vector &v) const noexcept
Whether the entries of v are equal to these, within epsilon.
Iterator get_itor() noexcept
NumType scalar_product(const Vector &v) const and AlMultiplicativeNumber< NumType >
The scalar product of this vector and v, over the same domain.
std::pair< T, NumType > Item_Type
Vector operator-() const
The negation of this vector.
void insert_or_remove_entry(const T &i, const NumType &value, Pair *ptr)
const DynList< Pair > format
const Domain & get_domain() const noexcept
Get the domain over which this vector is defined.
std::pair< T, NumType > Pair
const DynList< std::string > entries_str
Vector(Domain &&d, const DynList< NumType > &l, const NumType &zero=default_epsilon)
Construct vector from a temporary domain, which it keeps, and a list of values.
Const_Iterator get_itor() const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Vector operator*(const NumType &scalar) const
This vector multiplied by scalar.
return dstr n std::string(dstr.size(), '-')+"\n"+estr
std::pair< DynList< std::string >, DynList< std::string > > ret
bool are_equal(const NumType &n1, const NumType &n2) const noexcept
Check if two numeric values are equal within epsilon.
void set_entry(const T &i, const NumType &value)
Set a vector entry at given index.
const NumType & get_epsilon() const noexcept
Get the epsilon value used for zero comparisons.
Vector(const Domain &d, const NumType &zero=default_epsilon)
Construct a vector over a given domain (reference version)
Vector & divide_by_scalar(const NumType &scalar)
Divide every entry by scalar, which must not be zero.
NumType * search_entry(const T &i) noexcept
Search for an entry and return pointer to its value.
Vector & operator=(const Vector &v)
Copy assignment operator.
Aleph::Const_Iterator< Iterator > Const_Iterator
Read-only iterator: the iterator of a constant vector.
DynList< NumType > to_list() const
The values of the vector, in the order of the sorted keys of its domain.
Vector(const Vector &v)
Copy constructor.
static NumType abs(const NumType &val) noexcept
std::pair< std::string, std::string > Pair
bool operator!=(const Vector &v) const noexcept
Whether some entry of v differs from this one, beyond epsilon.
Vector & product_by_scalar(const NumType &scalar) noexcept
Multiply every entry by scalar.
Vector operator+(const Vector &r) const
The sum of this vector and r, over the same domain.
bool is_zero(const NumType &val) const noexcept
static bool test_epsilon(const NumType &e) noexcept
Vector(I, const NumType &=default_epsilon)=delete
A vector needs a domain: Vector(IntRange(n)), not Vector(n), which would turn n into an empty domain.
Vector(I, const DynList< NumType > &, const NumType &=default_epsilon)=delete
A vector needs a domain: Vector(IntRange(n), l), not Vector(n, l), which would turn n into an empty d...
Iterator get_it() noexcept
void set_entries(const Container< T > &c, std::initializer_list< NumType > lr)
Set multiple entries using container and initializer list.
NumType get_entry(const T &i) const noexcept
Get vector entry at given index (const version)
NumType operator()(const T &k) const noexcept
The value of the entry of k, read-only: 0 if it has none.
NumType operator[](const T &k) const noexcept
The value of the entry of k, read-only: 0 if it has none.
bool equal_to(const Vector &other) const noexcept
Check if this vector equals another within epsilon tolerance.
void set_epsilon(const NumType &e) noexcept
Set the epsilon value for zero comparisons.
Vector(Domain &&d, const NumType &zero=default_epsilon)
Construct a vector over a temporary domain, which it keeps.
Vector & operator+=(const Vector &v)
Add v, over the same domain, to this vector.
Vector(Vector &&v) noexcept
Move constructor.
std::shared_ptr< Domain > DomainPtr
static const NumType default_epsilon
Vector & operator-=(const Vector &v)
Subtract v, over the same domain, from this vector.
NumType get_entry(const T &i)
Get vector entry at given index (non-const version)
void set_entries(std::initializer_list< T > ld, std::initializer_list< NumType > lr)
Set multiple entries using initializer lists.
Const_Iterator get_it() const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
const DomainPtr & get_domain_ptr() const noexcept
Return the shared pointer to the domain.
void print() const and AlStreamable< T > and AlStreamable< NumType >
Print the nonzero entries, in the order of the sorted keys.
const NumType * search_entry(const T &i) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Vector(const Domain &d, const DynList< NumType > &l, const NumType &zero=default_epsilon)
Construct vector from domain and list of values (reference version)
Aleph::HashMap< T, NumType, MapODhash > Map
std::string to_str() const and AlStreamable< T > and
The keys over their values, in the order of the keys.
Vector operator/(const NumType &scalar) const
This vector divided by scalar, which must not be zero.
Vector(DomainPtr d, const DynList< NumType > &l, const NumType &zero=default_epsilon)
Construct vector from domain and list of values (shared_ptr version)
A number that adds and subtracts: what the sums, the differences and the comparisons of vectors and m...
A number that divides: what the division of a vector by a scalar needs.
A key of a domain: of an AlDomain, and so an index of a Vector or of a Matrix.
A number that multiplies: what the products by a scalar, and the products of vectors and matrices,...
A number of the entries of a Vector or of a Matrix.
A key of a domain that < orders: what AlDomain::keys(), and the members that take the keys in their o...
A value that << writes to a stream: what the members that print keys or numbers need.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
ZipIterator< Cs... > zip_it(const Cs &...cs)
Alias for get_zip_it.
auto unzip(const Container &l)
Separate a list of pairs into two lists.
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zip(const Container1 &a, const Container2 &b)
Zip two containers into a list of pairs.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
Matrix< Trow, Tcol, NumType > operator*(const NumType &scalar, const Matrix< Trow, Tcol, NumType > &m)
Scalar-matrix multiplication (scalar * matrix).
std::ostream & operator<<(std::ostream &osObject, const Field< T > &rightOp)
Iterator(const Vector &)=delete
A constant vector is iterated through Const_Iterator.
Iterator(Vector &vec)
Iterator on the entries of the modifiable vector vec.
Aleph::DynList< T > keys() const
See items().
Unified hash table interface.