UFJF - Machine Learning Toolkit  0.51.8
Point.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 #ifndef NOMINMAX
8  #define NOMINMAX
9 #endif
10 #undef min
11 #undef max
12 
13 #include <cstddef>
14 #include <cassert>
15 #include <vector>
16 #include <iostream>
17 #include <functional>
18 #include <cmath>
19 #include <memory>
20 #include <algorithm>
21 #include <initializer_list>
22 
23 #include "ExprOps.hpp"
24 #include "ExprScalar.hpp"
25 #include "Utils.hpp"
26 #include "Random.hpp"
27 
31 namespace mltk {
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 ;
36 
37 
41  template <typename T = double, typename Rep = std::vector<T> >
42  class Point {
43  private:
45  Rep x; // (access to) the data of the array
47  double y = -std::numeric_limits<double>::max(), alpha = 0.0;
49  size_t id = 0;
50 
51  public:
52  using Matrix = typename mltk::Point<mltk::Point<T>>;
53 
54  auto begin() const {
55  return x.cbegin();
56  }
57  auto end() const{
58  return x.cend();
59  }
60  auto begin() {
61  return x.begin();
62  }
63  auto end() {
64  return x.end();
65  }
69  Point(){}
70 
71  Point(Point<T> const &p){
72  this->x = p.X();
73  this->y = p.Y();
74  this->id = p.Id();
75  this->alpha = p.Alpha();
76  }
77 
78  template<typename R1>
79  Point(Point<T, R1> const &p){
80  if(p.size() > size()){
81  x.resize(p.size());
82  }
83  for(size_t i = 0; i < p.size(); i++){
84  this->x[i] = p[i];
85  }
86  this->y = p.Y();
87  this->id = p.Id();
88  this->alpha = p.Alpha();
89  }
90 
91  template<typename T2, typename R1>
92  Point(Point<T2, R1> const &p){
93  if(p.size() > size()){
94  x.resize(p.size());
95  }
96  for(size_t i = 0; i < p.size(); i++){
97  this->x[i] = p[i];
98  }
99  this->y = p.Y();
100  this->id = p.Id();
101  this->alpha = p.Alpha();
102  }
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) {}
119  Point(Rep const& rb): x(rb) {}
120 
121  Point(std::initializer_list<T> init): x(init) {}
122 
123  ~Point() = default;
124 
125  /*********************************************
126  * Getters *
127  *********************************************/
128 
133  [[nodiscard]] std::size_t size() const {
134  return x.size();
135  }
139  Rep const& X() const{
140  return x;
141  }
145  Rep& X(){
146  return x;
147  }
152  [[nodiscard]] double const& Y() const{
153  return y;
154  }
159  double& Y(){
160  return y;
161  }
166  [[nodiscard]] double const& Alpha() const{
167  return alpha;
168  }
173  double& Alpha(){
174  return alpha;
175  }
180  [[nodiscard]] size_t const& Id() const{
181  return id;
182  }
187  size_t& Id(){
188  return id;
189  }
190 
191  /*********************************************
192  * Setters *
193  *********************************************/
194 
199  void setY(double const& _y){
200  this->y = _y;
201  }
206  void setAlpha(double const& _alpha){
207  this->alpha = _alpha;
208  }
213  void setID(size_t const& _id){
214  this->id = _id;
215  }
216 
217  Point<T> selectFeatures(std::vector<size_t> feats) const {
218  Point<T> p(feats.size());
219  std::sort(feats.begin(), feats.end());
220 
221  size_t i = 0;
222  for(auto const& feat: feats){
223  assert(feat < x.size());
224  p[i] = x[feat];
225  i++;
226  }
227  p.Y() = y;
228  p.Id() = id;
229  p.Alpha() = alpha;
230  return p;
231  }
232 
233  template<typename Iter>
234  Iter erase(Iter b, Iter e){
235  return this->x.erase(b, e);
236  }
237 
238  [[nodiscard]] bool empty() const {
239  return this->x.empty();
240  }
241 
242  /*********************************************
243  * Access operators *
244  *********************************************/
245 
246  // index operator for constants and variables
247  decltype(auto) operator[] (const size_t &idx) const {
248  assert(idx < size());
249  return x[idx];
250  }
251 
252  T& operator[](const size_t &idx) {
253  assert(idx < size());
254  return x[idx];
255  }
256 
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()));
260  }
261 
262  template <typename... Types>
263  void resize(Types... args){
264  this->x.resize(args...);
265  }
266  template <typename... Types>
267  void assign(Types... args){
268  this->x.assign(args...);
269  }
270 
271  void clear(){
272  this->x.clear();
273  }
279  [[nodiscard]] double norm (int p = NormType::NORM_L2) const;
280 
285  T sum(const std::function <T (T)>& f = [](T const& t) { return t;}) const{
286  T _sum = T();
287 
288  for(std::size_t i = 0; i < size(); i++){
289  _sum += f(x[i]);
290  }
291 
292  return _sum;
293  }
294 
295  /*********************************************
296  * Other operators *
297  *********************************************/
298 
299  // assignment operator from same type
300  Point& operator=(Point const& b) {
301  if(b.size() > size()){
302  x.resize(b.size());
303  }
304 
305  for(std::size_t idx = 0; idx < b.size(); ++idx) {
306  x[idx] = b[idx];
307  }
308 
309  return *this;
310  }
311 
312  // assignment operator for arrays of different types
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){
317  x[idx] = b[idx];
318  }
319 
320  return *this;
321  }
322 
323  Point& operator=(std::vector<T> const& b) {
324  if(b.size() > size()){
325  x.resize(b.size());
326  }
327 
328  for(std::size_t idx = 0; idx < b.size(); idx++) {
329  x[idx] = b[idx];
330  }
331 
332  return *this;
333  }
334 
335  template< typename T2 >
336  Point& operator=(std::vector<T2> const& b) {
337  if(b.size() > size()){
338  x.resize(b.size());
339  }
340 
341  for(std::size_t idx = 0; idx < b.size(); ++idx) {
342  x[idx] = b[idx];
343  }
344 
345  return *this;
346  }
347 
348  Point& operator=(T const& b) {
349  for(std::size_t idx = 0; idx < size(); ++idx){
350  x[idx] = b;
351  }
352  return *this;
353  }
354 
355  template <typename Y>
356  Point& operator=(Y const& b) {
357  for(std::size_t idx = 0; idx < size(); ++idx){
358  x[idx] = b;
359  }
360  return *this;
361  }
362 
363  // plus assignment operator for arrays of same types
364  Point& operator+=(Point const& b) {
365  assert(size() == b.size());
366  for(std::size_t idx = 0; idx < b.size(); ++idx){
367  x[idx] += b[idx];
368  }
369  return *this;
370  }
371 
372  Point& operator+=(T const& b) {
373  for(std::size_t idx = 0; idx < size(); ++idx){
374  x[idx] += b;
375  }
376  return *this;
377  }
378 
379  template <typename Y>
380  Point& operator+=(Y const& b) {
381  for(std::size_t idx = 0; idx < size(); ++idx){
382  x[idx] += b;
383  }
384  return *this;
385  }
386 
387  // plus assignment operator for arrays of different types
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){
392  x[idx] += b[idx];
393  }
394  return *this;
395  }
396 
397  // minus assignment operator for arrays of same types
398  Point& operator-=(Point const& b) {
399  assert(size() == b.size());
400  for(std::size_t idx = 0; idx < b.size(); ++idx){
401  x[idx] -= b[idx];
402  }
403  return *this;
404  }
405 
406  // minus assignment operator for arrays of different types
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){
411  x[idx] -= b[idx];
412  }
413  return *this;
414  }
415 
416  Point& operator-=(T const& b) {
417  for(std::size_t idx = 0; idx < size(); ++idx){
418  x[idx] -= b;
419  }
420  return *this;
421  }
422 
423  template <typename Y>
424  Point& operator-=(Y const& b) {
425  for(std::size_t idx = 0; idx < size(); ++idx){
426  x[idx] -= b;
427  }
428  return *this;
429  }
430 
431  // minus assignment operator for arrays of same types
432  Point& operator*=(Point const& b) {
433  assert(size() == b.size());
434  for(std::size_t idx = 0; idx < b.size(); ++idx){
435  x[idx] *= b[idx];
436  }
437  return *this;
438  }
439 
440  // minus assignment operator for arrays of different types
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){
445  x[idx] *= b[idx];
446  }
447  return *this;
448  }
449 
450  Point& operator*=(T const& b) {
451  for(std::size_t idx = 0; idx < size(); ++idx){
452  x[idx] *= b;
453  }
454  return *this;
455  }
456 
457  template <typename Y>
458  Point& operator*=(Y const& b) {
459  for(std::size_t idx = 0; idx < size(); ++idx){
460  x[idx] *= b;
461  }
462  return *this;
463  }
464 
465  // minus assignment operator for arrays of same types
466  Point& operator/=(Point const& b) {
467  assert(size() == b.size());
468  for(std::size_t idx = 0; idx < b.size(); ++idx){
469  x[idx] /= b[idx];
470  }
471  return *this;
472  }
473 
474  // minus assignment operator for arrays of different types
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){
479  x[idx] /= b[idx];
480  }
481  return *this;
482  }
483 
484  Point& operator/=(T const& b) {
485  for(std::size_t idx = 0; idx < size(); ++idx){
486  x[idx] /= b;
487  }
488  return *this;
489  }
490 
491  template <typename Y>
492  Point& operator/=(Y const& b) {
493  for(std::size_t idx = 0; idx < size(); ++idx){
494  x[idx] /= b;
495  }
496  return *this;
497  }
498 
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;
502  };
503 
504  /*************************************************
505  * Overloaded operators for the Point template. *
506  *************************************************/
507 
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...);
511  }
512 
513  /*********************************************
514  * Point functions *
515  *********************************************/
516  template<typename T>
517  double norm(const Point<T>& p, int q){
518  return p.norm(q);
519  }
520 
527  template < typename T, typename R >
528  T dot (const Point<T, R> &p, const Point<T, R> &p1){
529  assert(p.size() == p1.size());
530  size_t dim = p.size();
531  T result = 0;
532  for(size_t i = 0; i < dim; i++){
533  result += p[i] * p1[i];
534  }
535 
536  return result;
537  }
538 
543  template < typename T, typename R >
544  T max(const Point<T, R> &p){
545  T _max = std::numeric_limits<T>::min();
546  for(size_t i = 0; i < p.size(); i++){
547  if(p[i] > _max) _max = p[i];
548  }
549  return _max;
550  }
551 
556  template < typename T, typename R >
557  T min(const Point<T, R> &p){
558  T _min = std::numeric_limits<T>::max();
559  for(size_t i = 0; i < p.size(); i++){
560  if(p[i] < _min) _min = p[i];
561  }
562  return _min;
563  }
564 
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){
567  mltk::random::init(seed);
568 
569  p.X().resize(size);
570  for(size_t i = 0; i < p.size(); i++){
571  p[i] = mltk::random::floatInRange<T>(0.0, 1.0);
572  }
573  p.Y() = 0;
574  return p;
575  }
576 
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){
579  Point<T, R> p(size);
580  mltk::random::init(seed);
581 
582  for(size_t i = 0; i < p.size(); i++){
583  p[i] = mltk::random::floatInRange(0.0, 1.0);
584  }
585  p.Y() = 0;
586  return p;
587  }
588 
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){
591  Point<T> p(size);
592  mltk::random::init(seed);
593 
594  for(size_t i = 0; i < p.size(); i++){
595  p[i] = mltk::random::floatInRange<T, distribution>(val1, val2);
596  }
597  p.Y() = 0;
598  return p;
599  }
600 
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){
603  mltk::random::init(seed);
604 
605  for(size_t i = 0; i < p.size(); i++){
606  p[i] = mltk::random::floatInRange(0.0, 1.0);
607  }
608  p.Y() = 0;
609  return p;
610  }
611 
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()));
615  }
616 
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()));
620  }
621 
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()));
625  }
626 
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()));
630  }
631 
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()));
635  }
636 
637 // template < typename T, typename R>
638 // Point<T, F_Pow<T, T, R> > pow(const Point<T, R>& p, const T &power){
639 // return Point<T, F_Pow<T, T, R > >(F_Pow<T, T, R>(p.X(), power));
640 // }
641 
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));
646  }
647  return Point<T, F_Pow<T, P, R > >(F_Pow<T, P, R>(p.X(), power));
648  }
649 
650  template<typename T, typename Rep>
651  double mltk::Point<T, Rep>::norm (int p) const{
652  if(p == NormType::NORM_LINF){
653  return mltk::max(mltk::abs(*this));
654  }
655  return std::pow(mltk::pow(mltk::abs(*this), p).sum(), 1.0/p);
656  }
662  template < typename T>
663  Point<T> normalize (Point<T> p, const double q){
664  double norm = std::pow(mltk::pow(mltk::abs(p), q).sum(), 1.0/q);
665  p /= norm;
666  return p;
667  }
668 
669  template < typename T>
670  Point<T> normalize (std::vector<T> &p, const double q){
671  Point<T> result = p;
672  double norm = std::pow(mltk::pow(mltk::abs(result), q).sum(), 1.0/q);
673  result /= norm;
674  p = result.X();
675  return result;
676  }
677 
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;
683  T val;
684  for (x = xs.begin(), val = lower; x != xs.end(); ++x, val += h) {
685  *x = val;
686  }
687  return xs;
688  }
689 
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();
693 
694  if(p.id > 0)
695  output << p.id << ":[";
696  else
697  output << "[";
698 
699  for(i = 0; i < dim-1; ++i){
700  output << p.x[i] << ", ";
701  }
702 
704  output << p.x[i] << ", " << p.y << "]";
705  else
706  output << p.x[i] << "]";
707 
708  return output;
709  }
710 
711  // equality of two points
712  template <typename T, typename R>
713  bool Point<T, R>::operator== (Point<T, R> const& rhs) const{
714  return x == rhs.x;
715  }
716 
717  // difference of two points
718  template< typename T, typename R >
719  bool Point< T, R >::operator!=(const Point<T, R> &rhs) const {
720  return !(rhs == *this);
721  }
722 
723  // module of point and scalar
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)));
727  }
728 
729  // module of point and scalar
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)));
733  }
734 
735  // adition of two points
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()));
739  }
740 
741  // addition of scalar and point
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()));
745  }
746 
747  // addition of scalar and point
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()));
751  }
752 
753  // addition of point and scalar
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)));
757  }
758 
759  // addition of point and scalar
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)));
763  }
764 
765  // subtraction of two points
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()));
769  }
770 
771  // subtraction of scalar and point
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()));
775  }
776 
777  // subtraction of scalar and point
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()));
781  }
782 
783  // subtraction of point and scalar
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)));
787  }
788 
789  // subtraction of point and scalar
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)));
793  }
794 
795  // multiplication of two points
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()));
799  }
800 
801  // multiplication of scalar and point
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()));
805  }
806 
807  // multiplication of scalar and point
808 // template<typename T, typename T2, typename R2>
809 // Point<T, A_Mult<T,A_Scalar<T2>,R2> > operator* (T2 const& s, Point<T,R2> const& b) {
810 // return Point<T,A_Mult<T,A_Scalar<T2>,R2>>(A_Mult<T,A_Scalar<T2>,R2>(A_Scalar<T2>(s), b.X()));
811 // }
812  // multiplication of scalar and point
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()));
816  }
817 
818  // multiplication of point and scalar
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)));
822  }
823 
824  // multiplication of point and scalar
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)));
828  }
829 
830  // division of two points
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()));
834  }
835 
836  // division of point and scalar
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)));
840  }
841 
842  // division of point and scalar
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)));
846  }
847 
848  // division of scalar and point
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()));
852  }
853 
854  // division of scalar and point
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()));
858  }
859 
860 }
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