21 #include <initializer_list>
23 #include "ExprOps.hpp"
24 #include "ExprScalar.hpp"
32 template <
typename T,
typename Rep >
class Point;
33 template <
typename T,
typename Rep> std::ostream &operator<<( std::ostream &output,
const Point<T, Rep> &p );
34 template <
class T,
typename Rep = std::vector<T> >
using PointPointer = std::shared_ptr<mltk::Point<T, Rep> >;
35 template <
class T,
typename Rep = std::vector<T> >
using PointIterator =
typename Rep::iterator ;
41 template <
typename T =
double,
typename Rep = std::vector<T> >
75 this->alpha = p.
Alpha();
79 Point(Point<T, R1>
const &p){
80 if(p.size() >
size()){
83 for(
size_t i = 0; i < p.size(); i++){
88 this->alpha = p.Alpha();
91 template<
typename T2,
typename R1>
92 Point(Point<T2, R1>
const &p){
93 if(p.size() >
size()){
96 for(
size_t i = 0; i < p.size(); i++){
101 this->alpha = p.Alpha();
107 explicit Point(std::size_t s): x(s) {}
114 Point(std::size_t s,
const T &value,
const std::size_t &
id = 0): x(s, value), id(id) {}
133 [[nodiscard]] std::size_t
size()
const {
139 Rep
const&
X()
const{
152 [[nodiscard]]
double const&
Y()
const{
166 [[nodiscard]]
double const&
Alpha()
const{
180 [[nodiscard]]
size_t const&
Id()
const{
207 this->alpha = _alpha;
217 Point<T> selectFeatures(std::vector<size_t> feats)
const {
219 std::sort(feats.begin(), feats.end());
222 for(
auto const& feat: feats){
223 assert(feat < x.size());
233 template<
typename Iter>
234 Iter erase(Iter b, Iter e){
235 return this->x.erase(b, e);
238 [[nodiscard]]
bool empty()
const {
239 return this->x.empty();
247 decltype(
auto) operator[] (const
size_t &idx)
const {
248 assert(idx <
size());
252 T& operator[](
const size_t &idx) {
253 assert(idx <
size());
257 template<
typename T2,
typename R2>
258 Point<T, A_Subscript<T, Rep, R2>> operator[](Point<T2, R2>
const& b) {
259 return Point<T, A_Subscript<T, Rep, R2>> (A_Subscript<T, Rep, R2>((*this).X(), b.X()));
262 template <
typename... Types>
263 void resize(Types... args){
264 this->x.resize(args...);
266 template <
typename... Types>
267 void assign(Types... args){
268 this->x.assign(args...);
279 [[nodiscard]]
double norm (
int p = NormType::NORM_L2)
const;
285 T
sum(
const std::function <T (T)>& f = [](T
const& t) {
return t;})
const{
288 for(std::size_t i = 0; i <
size(); i++){
301 if(b.size() >
size()){
305 for(std::size_t idx = 0; idx < b.size(); ++idx) {
313 template <
typename T2,
typename Rep2 >
314 Point& operator=(Point<T2, Rep2>
const& b) {
315 assert(
size() == b.size());
316 for(std::size_t idx = 0; idx <
size(); ++idx){
323 Point& operator=(std::vector<T>
const& b) {
324 if(b.size() >
size()){
328 for(std::size_t idx = 0; idx < b.size(); idx++) {
335 template<
typename T2 >
336 Point& operator=(std::vector<T2>
const& b) {
337 if(b.size() >
size()){
341 for(std::size_t idx = 0; idx < b.size(); ++idx) {
348 Point& operator=(T
const& b) {
349 for(std::size_t idx = 0; idx <
size(); ++idx){
355 template <
typename Y>
356 Point& operator=(
Y const& b) {
357 for(std::size_t idx = 0; idx <
size(); ++idx){
365 assert(
size() == b.size());
366 for(std::size_t idx = 0; idx < b.size(); ++idx){
372 Point& operator+=(T
const& b) {
373 for(std::size_t idx = 0; idx <
size(); ++idx){
379 template <
typename Y>
380 Point& operator+=(
Y const& b) {
381 for(std::size_t idx = 0; idx <
size(); ++idx){
388 template <
typename T2,
typename Rep2 >
389 Point& operator+=(Point<T2, Rep2>
const& b) {
390 assert(
size() == b.size());
391 for(std::size_t idx = 0; idx < b.size(); ++idx){
399 assert(
size() == b.size());
400 for(std::size_t idx = 0; idx < b.size(); ++idx){
407 template <
typename T2,
typename Rep2 >
408 Point& operator-=(Point<T2, Rep2>
const& b) {
409 assert(
size() == b.size());
410 for(std::size_t idx = 0; idx < b.size(); ++idx){
416 Point& operator-=(T
const& b) {
417 for(std::size_t idx = 0; idx <
size(); ++idx){
423 template <
typename Y>
424 Point& operator-=(
Y const& b) {
425 for(std::size_t idx = 0; idx <
size(); ++idx){
433 assert(
size() == b.size());
434 for(std::size_t idx = 0; idx < b.size(); ++idx){
441 template <
typename T2,
typename Rep2 >
442 Point& operator*=(Point<T2, Rep2>
const& b) {
443 assert(
size() == b.size());
444 for(std::size_t idx = 0; idx < b.size(); ++idx){
450 Point& operator*=(T
const& b) {
451 for(std::size_t idx = 0; idx <
size(); ++idx){
457 template <
typename Y>
458 Point& operator*=(
Y const& b) {
459 for(std::size_t idx = 0; idx <
size(); ++idx){
467 assert(
size() == b.size());
468 for(std::size_t idx = 0; idx < b.size(); ++idx){
475 template <
typename T2,
typename Rep2 >
476 Point& operator/=(Point<T2, Rep2>
const& b) {
477 assert(
size() == b.size());
478 for(std::size_t idx = 0; idx < b.size(); ++idx){
484 Point& operator/=(T
const& b) {
485 for(std::size_t idx = 0; idx <
size(); ++idx){
491 template <
typename Y>
492 Point& operator/=(
Y const& b) {
493 for(std::size_t idx = 0; idx <
size(); ++idx){
499 friend std::ostream &operator<< <T, Rep>( std::ostream &output,
const Point< T, Rep> &p );
500 bool operator== (
Point const& rhs)
const;
501 bool operator!= (
Point const& rhs)
const;
508 template <
typename T,
typename R = std::vector<T>,
typename... Types>
509 PointPointer<T, R> make_point(Types... args){
510 return std::make_shared< Point< T, R > >(args...);
517 double norm(
const Point<T>& p,
int q){
527 template <
typename T,
typename R >
530 size_t dim = p.
size();
532 for(
size_t i = 0; i < dim; i++){
533 result += p[i] * p1[i];
543 template <
typename T,
typename R >
546 for(
size_t i = 0; i < p.
size(); i++){
547 if(p[i] > _max) _max = p[i];
556 template <
typename T,
typename R >
559 for(
size_t i = 0; i < p.
size(); i++){
560 if(p[i] < _min) _min = p[i];
565 template <
typename T =
double,
typename R = std::vector< T > >
566 Point<T, R> random_init(Point<T, R> &p,
const size_t &size,
const size_t &seed){
570 for(
size_t i = 0; i < p.size(); i++){
571 p[i] = mltk::random::floatInRange<T>(0.0, 1.0);
577 template <
typename T =
double,
typename R = std::vector< T > >
578 Point<T, R> random_init(
const size_t &size,
const size_t &seed=0){
582 for(
size_t i = 0; i < p.size(); i++){
589 template <
typename T =
double,
typename distribution = std::uniform_real_distribution<T> >
590 Point<T> random_init(T val1, T val2,
const size_t &size,
const size_t &seed=0){
594 for(
size_t i = 0; i < p.size(); i++){
595 p[i] = mltk::random::floatInRange<T, distribution>(val1, val2);
601 template <
typename T =
double,
typename R = std::vector< T > >
602 Point<T, R> random_init(Point<T, R> &p,
const size_t &seed=0){
605 for(
size_t i = 0; i < p.size(); i++){
612 template <
typename T,
typename R>
613 Point<T, F_Abs<T, R> > abs(
const Point<T, R>& p){
614 return Point<T, F_Abs<T, R > >(F_Abs<T, R>(p.X()));
617 template <
typename T,
typename R>
618 Point<T, F_Cos<T, R> > cos(
const Point<T, R>& p){
619 return Point<T, F_Cos<T, R > >(F_Cos<T, R>(p.X()));
622 template <
typename T,
typename R>
623 Point<T, F_Sin<T, R> > sin(
const Point<T, R>& p){
624 return Point<T, F_Sin<T, R > >(F_Sin<T, R>(p.X()));
627 template <
typename T,
typename R>
628 Point<T, F_Exp<T, R> > exp(
const Point<T, R>& p){
629 return Point<T, F_Exp<T, R > >(F_Exp<T, R>(p.X()));
632 template <
typename T,
typename R>
633 Point<T, F_Log<T, R> > log(
const Point<T, R>& p){
634 return Point<T, F_Log<T, R > >(F_Log<T, R>(p.X()));
642 template <
typename T,
typename P,
typename R>
643 Point<T, F_Pow<T, P, R> > pow(
const Point<T, R>& p,
const P &power){
644 if constexpr (std::is_same_v<P, T>) {
645 return Point<T, F_Pow<T, T, R > >(F_Pow<T, T, R>(p.X(), power));
647 return Point<T, F_Pow<T, P, R > >(F_Pow<T, P, R>(p.X(), power));
650 template<
typename T,
typename Rep>
652 if(p == NormType::NORM_LINF){
655 return std::pow(mltk::pow(mltk::abs(*
this), p).sum(), 1.0/p);
662 template <
typename T>
664 double norm = std::pow(mltk::pow(mltk::abs(p), q).sum(), 1.0/q);
669 template <
typename T>
670 Point<T>
normalize (std::vector<T> &p,
const double q){
672 double norm = std::pow(mltk::pow(mltk::abs(result), q).sum(), 1.0/q);
678 template <
typename T =
double>
679 Point<T> linspace(
double lower,
double upper,
size_t N){
680 double h = (upper - lower) /
static_cast<double>(N-1);
681 std::vector<T> xs(N);
682 typename std::vector<T>::iterator x;
684 for (x = xs.begin(), val = lower; x != xs.end(); ++x, val += h) {
690 template <
typename T,
typename R>
691 std::ostream &operator<<( std::ostream &output,
const Point<T, R> &p ) {
692 int i, dim = p.x.size();
695 output << p.id <<
":[";
699 for(i = 0; i < dim-1; ++i){
700 output << p.x[i] <<
", ";
704 output << p.x[i] <<
", " << p.y <<
"]";
706 output << p.x[i] <<
"]";
712 template <
typename T,
typename R>
713 bool Point<T, R>::operator== (Point<T, R>
const& rhs)
const{
718 template<
typename T,
typename R >
719 bool Point< T, R >::operator!=(
const Point<T, R> &rhs)
const {
720 return !(rhs == *
this);
724 template<
typename T,
typename R1>
725 Point<T, A_Mod<T, R1, A_Scalar<T> > > operator%(
const Point<T, R1>& p,
const T& mod){
726 return Point<T, A_Mod<T, R1, A_Scalar<T>> >(A_Mod<T, R1, A_Scalar<T>>(p.X(), A_Scalar<T>(mod)));
730 template<
typename T,
typename Y,
typename R1>
731 Point<T, A_Mod<T, R1, A_Scalar<Y> > > operator%(
const Point<T, R1>& p,
const Y& mod){
732 return Point<T, A_Mod<T, R1, A_Scalar<Y>> >(A_Mod<T, R1, A_Scalar<Y>>(p.X(), A_Scalar<Y>(mod)));
736 template <
typename T,
typename R1,
typename R2>
737 Point<T, A_Add<T, R1, R2> > operator+ (Point<T, R1>
const& a, Point<T, R2>
const& b){
738 return Point<T, A_Add<T, R1, R2>>(A_Add<T, R1, R2>(a.X(), b.X()));
742 template<
typename T,
typename R2>
743 Point<T, A_Add<T,A_Scalar<T>,R2> > operator+ (T
const& s, Point<T,R2>
const& b) {
744 return Point<T,A_Add<T,A_Scalar<T>,R2>>(A_Add<T,A_Scalar<T>,R2>(A_Scalar<T>(s), b.X()));
748 template<
typename T,
typename T2,
typename R2>
749 Point<T, A_Add<T,A_Scalar<T2>,R2> > operator+ (T2
const& s, Point<T,R2>
const& b) {
750 return Point<T,A_Add<T,A_Scalar<T2>,R2>>(A_Add<T,A_Scalar<T2>,R2>(A_Scalar<T2>(s), b.X()));
754 template <
typename T,
typename R1>
755 Point<T, A_Add<T, R1, A_Scalar<T> > > operator+ (Point<T, R1>
const& a, T
const& s){
756 return Point<T, A_Add<T, R1, A_Scalar<T>>>(A_Add<T, R1, A_Scalar<T>>(a.X(), A_Scalar<T>(s)));
760 template <
typename T,
typename T2,
typename R1>
761 Point<T, A_Add<T, R1, A_Scalar<T2> > > operator+ (Point<T, R1>
const& a, T2
const& s){
762 return Point<T, A_Add<T, R1, A_Scalar<T2>>>(A_Add<T, R1, A_Scalar<T2>>(a.X(), A_Scalar<T2>(s)));
766 template <
typename T,
typename R1,
typename R2>
767 Point<T, A_Sub<T, R1, R2> > operator- (Point<T, R1>
const& a, Point<T, R2>
const& b){
768 return Point<T, A_Sub<T, R1, R2>>(A_Sub<T, R1, R2>(a.X(), b.X()));
772 template<
typename T,
typename R2>
773 Point<T, A_Sub<T,A_Scalar<T>,R2> > operator- (T
const& s, Point<T,R2>
const& b) {
774 return Point<T,A_Sub<T,A_Scalar<T>,R2>>(A_Sub<T,A_Scalar<T>,R2>(A_Scalar<T>(s), b.X()));
778 template<
typename T,
typename R2>
779 Point<T, A_Sub<T,A_Scalar<int>,R2> > operator- (
int const& s, Point<T,R2>
const& b) {
780 return Point<T,A_Sub<T,A_Scalar<int>,R2>>(A_Sub<T,A_Scalar<int>,R2>(A_Scalar<int>(s), b.X()));
784 template <
typename T,
typename R1>
785 Point<T, A_Sub<T, R1, A_Scalar<T> > > operator- (Point<T, R1>
const& a, T
const& s){
786 return Point<T, A_Sub<T, R1, A_Scalar<T>>>(A_Sub<T, R1, A_Scalar<T>>(a.X(), A_Scalar<T>(s)));
790 template <
typename T,
typename R1>
791 Point<T, A_Sub<T, R1, A_Scalar<int> > > operator- (Point<T, R1>
const& a,
int const& s){
792 return Point<T, A_Sub<T, R1, A_Scalar<int>>>(A_Sub<T, R1, A_Scalar<int>>(a.X(), A_Scalar<int>(s)));
796 template <
typename T,
typename R1,
typename R2>
797 Point<T, A_Mult<T, R1, R2> > operator* (Point<T, R1>
const& a, Point<T, R2>
const& b){
798 return Point<T, A_Mult<T, R1, R2>>(A_Mult<T, R1, R2>(a.X(), b.X()));
802 template<
typename T,
typename R2>
803 Point<T, A_Mult<T,A_Scalar<T>,R2> > operator* (T
const& s, Point<T,R2>
const& b) {
804 return Point<T,A_Mult<T,A_Scalar<T>,R2>>(A_Mult<T,A_Scalar<T>,R2>(A_Scalar<T>(s), b.X()));
813 template <
typename T,
typename T2,
typename R1>
814 Point<T, A_Mult<T, A_Scalar<T2>, R1> > operator* (T2
const& s, Point<T, R1>
const& b){
815 return Point<T, A_Mult<T, A_Scalar<T2>, R1>>(A_Mult<T, A_Scalar<T2>, R1>(A_Scalar<T2>(s), b.X()));
819 template <
typename T,
typename R1>
820 Point<T, A_Mult<T, R1, A_Scalar<T> > > operator* (Point<T, R1>
const& a, T
const& s){
821 return Point<T, A_Mult<T, R1, A_Scalar<T>>>(A_Mult<T, R1, A_Scalar<T>>(a.X(), A_Scalar<T>(s)));
825 template <
typename T,
typename T2,
typename R1>
826 Point<T, A_Mult<T, R1, A_Scalar<T2> > > operator* (Point<T, R1>
const& a, T2
const& s){
827 return Point<T, A_Mult<T, R1, A_Scalar<T2>>>(A_Mult<T, R1, A_Scalar<T2>>(a.X(), A_Scalar<T2>(s)));
831 template <
typename T,
typename R1,
typename R2>
832 Point<T, A_Div<T, R1, R2> > operator/ (Point<T, R1>
const & a, Point<T, R2>
const& b){
833 return Point<T, A_Div<T, R1, R2>>(A_Div<T, R1, R2>(a.X(), b.X()));
837 template <
typename T,
typename R1>
838 Point<T, A_Div<T, R1, A_Scalar<T> > > operator/ (Point<T, R1>
const& a, T
const& s){
839 return Point<T, A_Div<T, R1, A_Scalar<T>>>(A_Div<T, R1, A_Scalar<T>>(a.X(), A_Scalar<T>(s)));
843 template <
typename T,
typename R1>
844 Point<T, A_Div<T, R1, A_Scalar<int> > > operator/ (Point<T, R1>
const& a,
int const& s){
845 return Point<T, A_Div<T, R1, A_Scalar<int>>>(A_Div<T, R1, A_Scalar<int>>(a.X(), A_Scalar<int>(s)));
849 template<
typename T,
typename R2>
850 Point<T, A_Div<T,A_Scalar<T>,R2> > operator/ (T
const& s, Point<T,R2>
const& b) {
851 return Point<T,A_Div<T,A_Scalar<T>,R2>>(A_Div<T,A_Scalar<T>,R2>(A_Scalar<T>(s), b.X()));
855 template<
typename T,
typename R2>
856 Point<T, A_Div<T,A_Scalar<int>,R2> > operator/ (
int const& s, Point<T,R2>
const& b) {
857 return Point<T,A_Div<T,A_Scalar<int>,R2>>(A_Div<T,A_Scalar<int>,R2>(A_Scalar<int>(s), b.X()));
Wrapper for the point data.
Definition: Point.hpp:42
double norm(int p=NormType::NORM_L2) const
Returns the p-norm of the point.
Definition: Point.hpp:651
Rep const & X() const
Returns the attributes representation of the point (std::vector by default).
Definition: Point.hpp:139
void setAlpha(double const &_alpha)
Set alpha value of the point.
Definition: Point.hpp:206
void setID(size_t const &_id)
Set the id of the point.
Definition: Point.hpp:213
double & Y()
Returns a reference to the class or value of the point.
Definition: Point.hpp:159
Point(std::size_t s)
Construct a point with initial dimension.
Definition: Point.hpp:107
T sum(const std::function< T(T)> &f=[](T const &t) { return t;}) const
Compute the sum of the components of the point.
Definition: Point.hpp:285
double const & Alpha() const
Return the alpha value of the point.
Definition: Point.hpp:166
Point(Rep const &rb)
Construct a point with a custom internal representation.
Definition: Point.hpp:119
Point(std::size_t s, const T &value, const std::size_t &id=0)
Construct a point with initial dimension, default value and id.
Definition: Point.hpp:114
std::size_t size() const
Returns the dimension of the point.
Definition: Point.hpp:133
size_t const & Id() const
Returns the id of the point.
Definition: Point.hpp:180
double const & Y() const
Returns the class or value of the point.
Definition: Point.hpp:152
Rep & X()
Returns a reference to the attributes representation of the point (std::vector by default).
Definition: Point.hpp:145
double & Alpha()
Returns a reference to the alpha value of the point.
Definition: Point.hpp:173
Point()
Empty constructor.
Definition: Point.hpp:69
void setY(double const &_y)
Set the class or value of the point.
Definition: Point.hpp:199
size_t & Id()
Returns a reference to the id of the point.
Definition: Point.hpp:187
Real floatInRange(Real low, Real high)
Returns a float between low and high.
Definition: Random.cpp:34
size_t init(unsigned int seed=0)
Initialize the mersenne twister pseudorandom number generator.
Definition: Random.cpp:12
UFJF-MLTK main namespace for core functionalities.
Definition: classifier/Classifier.hpp:11
T min(const Point< T, R > &p)
Returns the min value of the point.
Definition: Point.hpp:557
T dot(const Point< T, R > &p, const Point< T, R > &p1)
Computes the dot product with a vector.
Definition: Point.hpp:528
T max(const Point< T, R > &p)
Returns the max value of the point.
Definition: Point.hpp:544
Point< T > normalize(Point< T > p, const double q)
normalize Normalize a vector using a Lp-norm.
Definition: Point.hpp:663