Yate
yatemath.h
1
22#ifndef __YATEMATH_H
23#define __YATEMATH_H
24
25#include <yateclass.h>
26#include <math.h>
27#include <string.h>
28
29namespace TelEngine {
30
31#ifdef DEBUG
32#ifdef _WINDOWS
33#define YMATH_FAIL(cond,...) { \
34 if (!(cond)) \
35 Debug(DebugFail,__VA_ARGS__); \
36}
37#else
38#define YMATH_FAIL(cond,args...) { \
39 if (!(cond)) \
40 Debug(DebugFail,args); \
41}
42#endif
43#else
44#ifdef _WINDOWS
45#define YMATH_FAIL do { break; } while
46#else
47#define YMATH_FAIL(arg...)
48#endif
49#endif
50
55class YATE_API Complex
56{
57public:
61 inline Complex()
62 : m_real(0), m_imag(0)
63 {}
64
70 inline Complex(float real, float imag = 0)
71 : m_real(real), m_imag(imag)
72 {}
73
78 inline Complex(const Complex& c)
79 : m_real(c.m_real), m_imag(c.m_imag)
80 {}
81
86 inline float re() const
87 { return m_real; }
88
93 inline void re(float r)
94 { m_real = r; }
95
100 inline float im() const
101 { return m_imag; }
102
107 inline void im(float i)
108 { m_imag = i; }
109
116 inline Complex& set(float r = 0, float i = 0) {
117 m_real = r;
118 m_imag = i;
119 return *this;
120 }
121
127 inline bool operator==(const Complex& c) const
128 { return m_real == c.m_real && m_imag == c.m_imag; }
129
135 inline bool operator!=(const Complex& c) const
136 { return m_real != c.m_real || m_imag != c.m_imag; }
137
143 inline Complex& operator=(const Complex& c)
144 { return set(c.m_real,c.m_imag); }
145
151 inline Complex& operator=(float real)
152 { return set(real); }
153
159 inline Complex& operator+=(const Complex& c)
160 { return set(m_real + c.m_real,m_imag + c.m_imag); }
161
167 inline Complex& operator+=(float real) {
168 m_real += real;
169 return *this;
170 }
171
177 inline Complex& operator-=(const Complex& c)
178 { return set(m_real - c.m_real,m_imag - c.m_imag); }
179
185 inline Complex& operator-=(float real) {
186 m_real -= real;
187 return *this;
188 }
189
195 inline Complex& operator*=(const Complex& c) {
196 return set(m_real * c.m_real - m_imag * c.m_imag,
197 m_real * c.m_imag + m_imag * c.m_real);
198 }
199
205 inline Complex& operator*=(float f)
206 { return set(m_real * f,m_imag * f); }
207
213 inline Complex& operator/=(const Complex& c) {
214 float tmp = c.norm2();
215 return set((m_real * c.m_real + m_imag * c.m_imag) / tmp,
216 (-m_real * c.m_imag + m_imag * c.m_real) / tmp);
217 }
218
224 inline Complex& operator/=(float f)
225 { return set(m_real / f,m_imag / f); }
226
231 inline float abs() const
232 { return ::sqrtf(norm2()); }
233
238 inline float mod() const
239 { return abs(); }
240
245 inline float arg() const
246 { return ::atan(m_imag / m_real); }
247
252 inline Complex exp() const {
253 float r = ::expf(m_real);
254 return Complex(r * ::cosf(m_imag),r * ::sinf(m_imag));
255 }
256
261 inline float norm() const
262 { return abs(); }
263
268 inline float norm2() const
269 { return m_real * m_real + m_imag * m_imag; }
270
271private:
272 float m_real; // The real part
273 float m_imag; // The imaginary part
274};
275
276
281class YATE_API RefStorage : public RefObject
282{
283 YCLASS(RefStorage,RefObject)
284 YNOCOPY(RefStorage); // No automatic copies please
285public:
291 inline RefStorage(const void* value, unsigned int len)
292 : m_data((void*)value,len)
293 {}
294
299 inline unsigned int length() const
300 { return m_data.length(); }
301
308 inline void* data(unsigned int offs, unsigned int len) const
309 { return len ? m_data.data(offs,len) : 0; }
310
319 inline bool set(const void* buf, unsigned int len, unsigned int offs = 0)
320 { return copy(data(offs,len),buf,len); }
321
328 static inline void fill(void* dest, unsigned int len, int val = 0) {
329 if (dest && len)
330 ::memset(dest,val,len);
331 }
332
340 static inline bool copy(void* dest, const void* src, unsigned int len) {
341 if (!(len && dest && src))
342 return len == 0;
343 if (dest != src)
344 ::memcpy(dest,src,len);
345 return true;
346 }
347
355 static inline bool equals(const void* buf1, const void* buf2, unsigned int len) {
356 if (len && buf1 && buf2)
357 return (buf1 == buf2) || (::memcmp(buf1,buf2,len) == 0);
358 return true;
359 }
360
373 static String& dumpSplit(String& buf, const String& str, unsigned int lineLen,
374 unsigned int offset = 0, const char* linePrefix = 0,
375 const char* suffix = "\r\n");
376
377private:
378 RefStorage() {}; // No default constructor please
379
380 DataBlock m_data;
381};
382
383
389class YATE_API MathVectorBase : public GenObject
390{
392public:
397 {}
398
403 virtual unsigned int vectorSize() const = 0;
404};
405
406
412template <class Obj> class SliceVector : public MathVectorBase
413{
414public:
418 inline SliceVector()
419 : m_storage(0), m_data(0), m_length(0), m_maxLen(0)
420 {}
421
427 inline SliceVector(const SliceVector& other)
428 : m_storage(0), m_data(0), m_length(0), m_maxLen(0)
429 { initSlice(false,other); }
430
439 explicit inline SliceVector(unsigned int len, const Obj* buf = 0,
440 unsigned int maxLen = 0)
441 : m_storage(0), m_data(0), m_length(0), m_maxLen(0)
442 { initStorage(len,buf,maxLen); }
443
450 explicit inline SliceVector(const SliceVector& v1, const SliceVector& v2)
451 : m_storage(0), m_data(0), m_length(0), m_maxLen(0) {
452 if (!initStorage(v1.length(),v1.data(),v1.length() + v2.length()))
453 return;
454 resizeMax();
455 m_storage->set(v2.data(),v2.size(),v1.size());
456 }
457
465 explicit inline SliceVector(const SliceVector& v1, const SliceVector& v2,
466 const SliceVector& v3)
467 : m_storage(0), m_data(0), m_length(0), m_maxLen(0) {
468 unsigned int n = v1.length() + v2.length() + v3.length();
469 if (!initStorage(v1.length(),v1.data(),n))
470 return;
471 resizeMax();
472 m_storage->set(v2.data(),v2.size(),v1.size());
473 m_storage->set(v3.data(),v3.size(),v1.size() + v2.size());
474 }
475
483 explicit inline SliceVector(const SliceVector& other, unsigned int offs,
484 unsigned int len = 0)
485 : m_storage(0), m_data(0), m_length(0), m_maxLen(0)
486 { initSlice(false,other,offs,len); }
487
491 virtual ~SliceVector()
492 { setData(); }
493
501 inline Obj* data(unsigned int offs, unsigned int len) {
502 if (len && length() && offs + len <= length())
503 return m_data + offs;
504 return 0;
505 }
506
514 inline const Obj* data(unsigned int offs, unsigned int len) const {
515 if (len && length() && offs + len <= length())
516 return m_data + offs;
517 return 0;
518 }
519
525 inline Obj* data(unsigned int offs = 0)
526 { return data(offs,available(offs)); }
527
533 inline const Obj* data(unsigned int offs = 0) const
534 { return data(offs,available(offs)); }
535
545 inline Obj* data(unsigned int offs, unsigned int len, Obj*& eod) {
546 Obj* d = data(offs,len);
547 eod = end(d,len);
548 return d;
549 }
550
560 inline const Obj* data(unsigned int offs, unsigned int len, const Obj*& eod) const {
561 const Obj* d = data(offs,len);
562 eod = end(d,len);
563 return d;
564 }
565
570 inline unsigned int length() const
571 { return m_length; }
572
578 inline unsigned int maxLen() const
579 { return m_maxLen; }
580
585 inline unsigned int size() const
586 { return size(length()); }
587
596 inline unsigned int available(unsigned int offs, int len = -1) const {
597 if (len && offs < length()) {
598 unsigned int rest = length() - offs;
599 return (len < 0 || rest <= (unsigned int)len) ? rest : (unsigned int)len;
600 }
601 return 0;
602 }
603
612 inline unsigned int availableClamp(unsigned int clamp, unsigned int offs = 0,
613 int len = -1) const {
614 offs = available(offs,len);
615 return clamp <= offs ? clamp : offs;
616 }
617
622 virtual unsigned int vectorSize() const
623 { return size(); }
624
631 inline bool resize(unsigned int len) {
632 if (len <= maxLen()) {
633 m_length = len;
634 return true;
635 }
636 return false;
637 }
638
642 inline void resizeMax()
643 { resize(maxLen()); }
644
649 inline void steal(SliceVector& other) {
650 m_storage = other.m_storage;
651 m_data = other.m_data;
652 m_length = other.m_length;
653 m_maxLen = other.m_maxLen;
654 other.m_storage = 0;
655 other.m_data = 0;
656 other.m_length = other.m_maxLen = 0;
657 }
658
665 inline void resetStorage(unsigned int len, unsigned int maxLen = 0) {
666 setData();
667 initStorage(len,0,maxLen);
668 }
669
677 inline bool setSlice(const SliceVector& other, unsigned int offs = 0,
678 unsigned int len = 0)
679 { return initSlice(true,other,offs,len); }
680
686 inline SliceVector head(unsigned int len) const
687 { return slice(0,len); }
688
695 inline bool head(SliceVector& dest, unsigned int len) const
696 { return slice(dest,0,len); }
697
703 inline SliceVector tail(unsigned int len) const {
704 if (len < length())
705 return SliceVector(*this,length() - len,len);
706 return SliceVector();
707 }
708
715 inline bool tail(SliceVector& dest, unsigned int len) const {
716 if (len <= length())
717 return dest.initSlice(true,*this,length() - len,len);
718 dest.setData();
719 return false;
720 }
721
729 inline SliceVector slice(unsigned int offs, unsigned int len) const
730 { return SliceVector(*this,offs,len); }
731
740 inline bool slice(SliceVector& dest, unsigned int offs,
741 unsigned int len = 0) const
742 { return dest.initSlice(true,*this,offs,len); }
743
754 inline bool copy(const SliceVector& src, unsigned int len,
755 unsigned int offs = 0, unsigned int srcOffs = 0)
756 { return RefStorage::copy(data(offs,len),src.data(srcOffs,len),size(len)); }
757
763 inline void bzero(unsigned int offs, unsigned int len)
764 { RefStorage::fill(data(offs,len),size(len)); }
765
769 inline void bzero()
770 { RefStorage::fill(data(),size()); }
771
776 inline void fill(const Obj& value) {
777 Obj* d = data();
778 for (Obj* last = end(d,length()); d != last; ++d)
779 *d = value;
780 }
781
786 inline void apply(void (*func)(Obj&)) {
787 Obj* d = data();
788 for (Obj* last = end(d,length()); d != last; ++d)
789 (*func)(*d);
790 }
791
796 inline Obj sum() const {
797 Obj result(0);
798 const Obj* d = data();
799 for (const Obj* last = end(d,length()); d != last; ++d)
800 result += *d;
801 return result;
802 }
803
809 inline Obj sumApply(Obj (*func)(const Obj&)) const {
810 Obj result(0);
811 const Obj* d = data();
812 for (const Obj* last = end(d,length()); d != last; ++d)
813 result += (*func)(*d);
814 return result;
815 }
816
822 inline float sumApplyF(float (*func)(const Obj&)) const {
823 float result = 0;
824 const Obj* d = data();
825 for (const Obj* last = end(d,length()); d != last; ++d)
826 result += (*func)(*d);
827 return result;
828 }
829
835 inline bool sum(const SliceVector& other) {
836 if (length() != other.length())
837 return false;
838 const Obj* od = other.m_data;
839 Obj* d = data();
840 for (Obj* last = end(d,length()); d != last; ++d, ++od)
841 *d += *od;
842 return true;
843 }
844
849 inline void sum(const Obj& value) {
850 Obj* d = data();
851 for (Obj* last = end(d,length()); d != last; ++d)
852 *d += value;
853 }
854
860 inline bool sub(const SliceVector& other) {
861 if (length() != other.length())
862 return false;
863 const Obj* od = other.m_data;
864 Obj* d = data();
865 for (Obj* last = end(d,length()); d != last; ++d, ++od)
866 *d -= *od;
867 return true;
868 }
869
874 inline void sub(const Obj& value) {
875 Obj* d = data();
876 for (Obj* last = end(d,length()); d != last; ++d)
877 *d -= value;
878 }
879
885 inline bool mul(const SliceVector& other) {
886 if (length() != other.length())
887 return false;
888 const Obj* od = other.m_data;
889 Obj* d = data();
890 for (Obj* last = end(d,length()); d != last; ++d, ++od)
891 *d *= *od;
892 return true;
893 }
894
899 inline void mul(const Obj& value) {
900 Obj* d = data();
901 for (Obj* last = end(d,length()); d != last; ++d)
902 *d *= value;
903 }
904
909 inline void mul(float value) {
910 Obj* d = data();
911 for (Obj* last = end(d,length()); d != last; ++d)
912 *d *= value;
913 }
914
920 inline Obj& operator[](unsigned int index) {
921 YMATH_FAIL(index < m_length,
922 "SliceVector::operator[] index out of bounds [%p]",this);
923 return m_data[index];
924 }
925
931 inline const Obj& operator[](unsigned int index) const {
932 YMATH_FAIL(index < m_length,
933 "SliceVector::operator[] index out of bounds [%p]",this);
934 return m_data[index];
935 }
936
942 inline Obj& operator[](signed int index) {
943 YMATH_FAIL((unsigned int)index < m_length,
944 "SliceVector::operator[] index out of bounds [%p]",this);
945 return m_data[index];
946 }
947
953 inline const Obj& operator[](signed int index) const {
954 YMATH_FAIL((unsigned int)index < m_length,
955 "SliceVector::operator[] index out of bounds [%p]",this);
956 return m_data[index];
957 }
958
964 inline bool operator==(const SliceVector& other) const
965 { return equals(other); }
966
972 inline bool operator!=(const SliceVector& other) const
973 { return !equals(other); }
974
980 inline SliceVector& operator=(const SliceVector& other) {
981 setSlice(other);
982 return *this;
983 }
984
990 inline SliceVector& operator+=(const SliceVector& other) {
991 YMATH_FAIL(length() == other.length(),
992 "SliceVector(+=): invalid lengths [%p]",this);
993 sum(other);
994 return *this;
995 }
996
1002 inline SliceVector& operator+=(const Obj& value) {
1003 sum(value);
1004 return *this;
1005 }
1006
1012 inline SliceVector& operator-=(const SliceVector& other) {
1013 YMATH_FAIL(length() == other.length(),
1014 "SliceVector(-=): invalid lengths [%p]",this);
1015 sub(other);
1016 return *this;
1017 }
1018
1024 inline SliceVector& operator-=(const Obj& value) {
1025 sub(value);
1026 return *this;
1027 }
1028
1034 inline SliceVector& operator*=(const SliceVector& other) {
1035 YMATH_FAIL(length() == other.length(),
1036 "SliceVector(*=): invalid lengths [%p]",this);
1037 mul(other);
1038 return *this;
1039 }
1040
1046 inline SliceVector& operator*=(const Obj& value) {
1047 mul(value);
1048 return *this;
1049 }
1050
1056 inline SliceVector& operator*=(float value) {
1057 mul(value);
1058 return *this;
1059 }
1060
1066 inline bool equals(const SliceVector& other) const {
1067 return length() == other.length() &&
1068 RefStorage::equals(data(),other.data(),size());
1069 }
1070
1081 String& (*func)(String& s, const Obj& o, const char* sep, const char* fmt),
1082 const char* sep = ",", const char* fmt = 0) const {
1083 const Obj* d = data();
1084 if (!(d && func))
1085 return buf;
1086 String localBuf;
1087 for (const Obj* last = end(d,length()); d != last; ++d)
1088 (*func)(localBuf,*d,sep,fmt);
1089 return buf.append(localBuf);
1090 }
1091
1107 String& dump(String& buf, unsigned int lineLen,
1108 String& (*func)(String& s, const Obj& o, const char* sep, const char* fmt),
1109 unsigned int offset = 0, const char* linePrefix = 0,
1110 const char* suffix = "\r\n", const char* sep = ",", const char* fmt = 0) const {
1111 const Obj* d = data();
1112 if (!(d && func))
1113 return buf.append(suffix);
1114 if (TelEngine::null(linePrefix))
1115 linePrefix = suffix;
1116 if (!lineLen || TelEngine::null(linePrefix))
1117 return dump(buf,func,sep,fmt) << suffix;
1118 String localBuf;
1119 for (const Obj* last = end(d,length()); d != last;) {
1120 String tmp;
1121 (*func)(tmp,*d,0,fmt);
1122 if (++d != last)
1123 tmp << sep;
1124 offset += tmp.length();
1125 if (offset > lineLen) {
1126 localBuf << linePrefix;
1127 offset = tmp.length();
1128 }
1129 localBuf << tmp;
1130 }
1131 return buf << localBuf << suffix;
1132 }
1133
1140 inline String& hexify(String& buf, char sep = 0) const
1141 { return buf.hexify((void*)data(),size(),sep); }
1142
1154 inline String& dumpHex(String& buf, unsigned int lineLen,
1155 unsigned int offset = 0, const char* linePrefix = 0,
1156 const char* suffix = "\r\n") const {
1157 String h;
1158 return RefStorage::dumpSplit(buf,hexify(h),lineLen,offset,linePrefix,suffix);
1159 }
1160
1176 int unHexify(const char* str, unsigned int len, int sep = 255) {
1177 setData();
1178 DataBlock db;
1179 bool ok = (sep < -128 || sep > 127) ? db.unHexify(str,len) :
1180 db.unHexify(str,len,(char)sep);
1181 if (ok && (db.length() % objSize() == 0)) {
1182 initStorage(db.length() / objSize(),(const Obj*)db.data(0,db.length()));
1183 return 0;
1184 }
1185 return ok ? 1 : -1;
1186 }
1187
1194 inline int unHexify(const String& str, int sep = 255)
1195 { return unHexify(str.c_str(),str.length(),sep); }
1196
1201 static inline unsigned int objSize()
1202 { return sizeof(Obj); }
1203
1209 static inline unsigned int size(unsigned int len)
1210 { return len * objSize(); }
1211
1212protected:
1213 // Return end-of-data pointer from start and given length
1214 inline Obj* end(Obj* start, unsigned int len)
1215 { return start ? (start + len) : 0; }
1216 inline const Obj* end(const Obj* start, unsigned int len) const
1217 { return start ? (start + len) : 0; }
1218 // Set data. Reset storage if we don't have a valid data pointer
1219 // Return true if we have valid data
1220 inline bool setData(Obj* data = 0, unsigned int len = 0, unsigned int maxLen = 0) {
1221 m_data = data;
1222 if (m_data) {
1223 m_length = len;
1224 m_maxLen = maxLen;
1225 }
1226 else {
1227 m_length = m_maxLen = 0;
1228 TelEngine::destruct(m_storage);
1229 }
1230 return m_data != 0;
1231 }
1232 // Build storage, update data. This method assumes our data is cleared
1233 // If data is given 'len' elements will be copied from it to storage
1234 inline bool initStorage(unsigned int len, const Obj* data = 0,
1235 unsigned int maxLen = 0) {
1236 if (maxLen < len)
1237 maxLen = len;
1238 if (!maxLen)
1239 return false;
1240 if (!data || maxLen == len)
1241 m_storage = new RefStorage(data,size(maxLen));
1242 else {
1243 m_storage = new RefStorage(0,size(maxLen));
1244 m_storage->set(data,size(len));
1245 }
1246 return setData((Obj*)m_storage->data(0,1),len,maxLen);
1247 }
1248 // Build storage from slice and update data.
1249 // Clear data if requested
1250 inline bool initSlice(bool del, const SliceVector& other, unsigned int offs = 0,
1251 unsigned int len = 0) {
1252 if (!len)
1253 len = other.length();
1254 Obj* d = (Obj*)other.data(offs,len);
1255 if (!d) {
1256 if (del)
1257 setData();
1258 return len == 0;
1259 }
1260 if (m_storage == other.m_storage)
1261 return setData(d,len,len);
1262 RefStorage* tmp = other.m_storage;
1263 if (tmp->ref()) {
1264 TelEngine::destruct(m_storage);
1265 m_storage = tmp;
1266 return setData(d,len,len);
1267 }
1268 Debug(DebugFail,"SliceVector storage ref() failed");
1269 return del ? setData() : false;
1270 }
1271
1272 RefStorage* m_storage; // Vector storage
1273 Obj* m_data; // Pointer to data
1274 unsigned int m_length; // Data length
1275 unsigned int m_maxLen; // Max storage
1276};
1277
1278typedef SliceVector<Complex> ComplexVector;
1279typedef SliceVector<float> FloatVector;
1280typedef SliceVector<uint8_t> ByteVector;
1281
1289class YATE_API BitVector : public ByteVector
1290{
1291public:
1295 inline BitVector()
1296 {}
1297
1303 inline BitVector(const BitVector& other)
1304 : ByteVector(other)
1305 {}
1306
1314 explicit inline BitVector(unsigned int len, unsigned int maxLen = 0)
1315 : ByteVector(len,0,maxLen)
1316 {}
1317
1325 explicit inline BitVector(const BitVector& other, unsigned int offs,
1326 unsigned int len = 0)
1327 : ByteVector(other,offs,len)
1328 {}
1329
1335 explicit BitVector(const char* str, unsigned int maxLen = 0);
1336
1341 bool valid() const;
1342
1349 bool get(FloatVector& dest) const;
1350
1357 bool set(const FloatVector& input);
1358
1367 void xorMsb(uint32_t value, unsigned int offs = 0, uint8_t len = 32);
1368
1377 inline void xorMsb16(uint16_t value, unsigned int offs = 0, uint8_t len = 16)
1378 { return xorMsb((uint32_t)value << 16,offs,len <= 16 ? len : 16); }
1379
1387 uint64_t pack(unsigned int offs = 0, int len = -1) const;
1388
1395 void unpack(uint64_t value, unsigned int offs = 0, uint8_t len = 64);
1396
1404 void unpackMsb(uint32_t value, unsigned int offs = 0, uint8_t len = 32);
1405
1413 inline void unpackMsb16(uint16_t value, unsigned int offs = 0, uint8_t len = 16)
1414 { unpackMsb((uint32_t)value << 16,offs,len <= 16 ? len : 16); }
1415
1424 bool pack(ByteVector& dest) const;
1425
1433 bool unpack(const ByteVector& src);
1434
1442 String& appendTo(String& buf, unsigned int offs = 0, int len = -1) const;
1443
1450 inline String toString(unsigned int offs, int len = -1) const {
1451 String tmp;
1452 return appendTo(tmp,offs,len);
1453 }
1454
1462 inline bool setSlice(const BitVector& other, unsigned int offs = 0,
1463 unsigned int len = 0)
1464 { return initSlice(true,other,offs,len); }
1465
1471 inline BitVector head(unsigned int len) const
1472 { return slice(0,len); }
1473
1480 inline bool head(BitVector& dest, unsigned int len) const
1481 { return slice(dest,0,len); }
1482
1488 inline BitVector tail(unsigned int len) const {
1489 if (len < length())
1490 return BitVector(*this,length() - len,len);
1491 return BitVector();
1492 }
1493
1500 inline bool tail(BitVector& dest, unsigned int len) const {
1501 if (len <= length())
1502 return dest.initSlice(true,*this,length() - len,len);
1503 dest.setData();
1504 return false;
1505 }
1506
1514 inline BitVector slice(unsigned int offs, unsigned int len) const
1515 { return BitVector(*this,offs,len); }
1516
1525 inline bool slice(BitVector& dest, unsigned int offs, unsigned int len = 0) const
1526 { return dest.initSlice(true,*this,offs,len); }
1527};
1528
1529
1534class YATE_API Math
1535{
1536public:
1545 static String& dumpComplex(String& buf, const Complex& val, const char* sep = 0,
1546 const char* fmt = 0);
1547
1556 static String& dumpFloat(String& buf, const float& val, const char* sep = 0,
1557 const char* fmt = 0);
1558};
1559
1560
1567inline Complex operator+(const Complex& c1, const Complex& c2)
1568{
1569 Complex tmp(c1);
1570 return (tmp += c2);
1571}
1572
1579inline Complex operator+(const Complex& c, float f)
1580{
1581 Complex tmp(c);
1582 return (tmp += f);
1583}
1584
1591inline Complex operator+(float f, const Complex& c)
1592{
1593 return operator+(c,f);
1594}
1595
1602inline Complex operator-(const Complex& c1, const Complex& c2)
1603{
1604 Complex tmp(c1);
1605 return (tmp -= c2);
1606}
1607
1614inline Complex operator-(const Complex& c, float f)
1615{
1616 Complex tmp(c);
1617 return (tmp -= f);
1618}
1619
1626inline Complex operator*(const Complex& c1, const Complex& c2)
1627{
1628 Complex tmp(c1);
1629 return (tmp *= c2);
1630}
1631
1638inline Complex operator*(const Complex& c, float f)
1639{
1640 Complex tmp(c);
1641 return (tmp *= f);
1642}
1643
1650inline Complex operator*(float f, const Complex& c)
1651{
1652 return operator*(c,f);
1653}
1654
1661inline Complex operator/(const Complex& c1, const Complex& c2)
1662{
1663 Complex tmp(c1);
1664 return (tmp /= c2);
1665}
1666
1673inline Complex operator/(const Complex& c, float f)
1674{
1675 Complex tmp(c);
1676 return (tmp /= f);
1677}
1678
1685inline String& operator<<(String& str, const Complex& c)
1686{
1687 return Math::dumpComplex(str,c);
1688}
1689
1696inline String& operator<<(String& str, const BitVector& b)
1697{
1698 return b.appendTo(str);
1699}
1700
1701}; // namespace TelEngine
1702
1703#endif /* __YATEMATH_H */
1704
1705/* vi: set ts=8 sw=4 sts=4 noet: */
A class that holds just a block of raw data.
Definition yateclass.h:6074
void * data() const
Definition yateclass.h:6119
bool unHexify(const char *data, unsigned int len, char sep)
Definition yateclass.h:6628
unsigned int length() const
Definition yateclass.h:6151
Definition yateclass.h:1627
Definition yateclass.h:1752
A C-style string handling class.
Definition yateclass.h:3055
const char * c_str() const
Definition yateclass.h:3160
String & hexify(const void *data, unsigned int len, char sep=0, bool upCase=false)
String & append(const char *value, int len)
unsigned int length() const
Definition yateclass.h:3182
A slice vector holding bits.
Definition yatemath.h:1290
bool head(BitVector &dest, unsigned int len) const
Definition yatemath.h:1480
String toString(unsigned int offs, int len=-1) const
Definition yatemath.h:1450
BitVector(unsigned int len, unsigned int maxLen=0)
Definition yatemath.h:1314
bool valid() const
bool unpack(const ByteVector &src)
uint64_t pack(unsigned int offs=0, int len=-1) const
void unpackMsb16(uint16_t value, unsigned int offs=0, uint8_t len=16)
Definition yatemath.h:1413
bool tail(BitVector &dest, unsigned int len) const
Definition yatemath.h:1500
BitVector(const BitVector &other, unsigned int offs, unsigned int len=0)
Definition yatemath.h:1325
BitVector(const char *str, unsigned int maxLen=0)
String & appendTo(String &buf, unsigned int offs=0, int len=-1) const
BitVector tail(unsigned int len) const
Definition yatemath.h:1488
void xorMsb(uint32_t value, unsigned int offs=0, uint8_t len=32)
BitVector()
Definition yatemath.h:1295
BitVector(const BitVector &other)
Definition yatemath.h:1303
void xorMsb16(uint16_t value, unsigned int offs=0, uint8_t len=16)
Definition yatemath.h:1377
void unpack(uint64_t value, unsigned int offs=0, uint8_t len=64)
BitVector head(unsigned int len) const
Definition yatemath.h:1471
bool pack(ByteVector &dest) const
BitVector slice(unsigned int offs, unsigned int len) const
Definition yatemath.h:1514
bool set(const FloatVector &input)
void unpackMsb(uint32_t value, unsigned int offs=0, uint8_t len=32)
bool setSlice(const BitVector &other, unsigned int offs=0, unsigned int len=0)
Definition yatemath.h:1462
bool slice(BitVector &dest, unsigned int offs, unsigned int len=0) const
Definition yatemath.h:1525
bool get(FloatVector &dest) const
A Complex (float) number.
Definition yatemath.h:56
float re() const
Definition yatemath.h:86
Complex & operator*=(const Complex &c)
Definition yatemath.h:195
Complex & operator=(const Complex &c)
Definition yatemath.h:143
Complex & operator+=(const Complex &c)
Definition yatemath.h:159
void im(float i)
Definition yatemath.h:107
float im() const
Definition yatemath.h:100
Complex & operator-=(const Complex &c)
Definition yatemath.h:177
bool operator!=(const Complex &c) const
Definition yatemath.h:135
bool operator==(const Complex &c) const
Definition yatemath.h:127
Complex(const Complex &c)
Definition yatemath.h:78
Complex(float real, float imag=0)
Definition yatemath.h:70
Complex & operator-=(float real)
Definition yatemath.h:185
Complex & operator*=(float f)
Definition yatemath.h:205
Complex & operator/=(const Complex &c)
Definition yatemath.h:213
void re(float r)
Definition yatemath.h:93
Complex()
Definition yatemath.h:61
Complex exp() const
Definition yatemath.h:252
Complex & operator+=(float real)
Definition yatemath.h:167
float norm2() const
Definition yatemath.h:268
float abs() const
Definition yatemath.h:231
float arg() const
Definition yatemath.h:245
Complex & set(float r=0, float i=0)
Definition yatemath.h:116
float mod() const
Definition yatemath.h:238
Complex & operator=(float real)
Definition yatemath.h:151
float norm() const
Definition yatemath.h:261
Complex & operator/=(float f)
Definition yatemath.h:224
Base class for vector class(es)
Definition yatemath.h:390
virtual unsigned int vectorSize() const =0
virtual ~MathVectorBase()
Definition yatemath.h:396
Math utilities.
Definition yatemath.h:1535
static String & dumpFloat(String &buf, const float &val, const char *sep=0, const char *fmt=0)
static String & dumpComplex(String &buf, const Complex &val, const char *sep=0, const char *fmt=0)
A fixed ref counted storage.
Definition yatemath.h:282
void * data(unsigned int offs, unsigned int len) const
Definition yatemath.h:308
bool set(const void *buf, unsigned int len, unsigned int offs=0)
Definition yatemath.h:319
static String & dumpSplit(String &buf, const String &str, unsigned int lineLen, unsigned int offset=0, const char *linePrefix=0, const char *suffix="\r\n")
static bool equals(const void *buf1, const void *buf2, unsigned int len)
Definition yatemath.h:355
RefStorage(const void *value, unsigned int len)
Definition yatemath.h:291
static bool copy(void *dest, const void *src, unsigned int len)
Definition yatemath.h:340
static void fill(void *dest, unsigned int len, int val=0)
Definition yatemath.h:328
unsigned int length() const
Definition yatemath.h:299
A slice vector.
Definition yatemath.h:413
void resizeMax()
Definition yatemath.h:642
String & dump(String &buf, unsigned int lineLen, String &(*func)(String &s, const Obj &o, const char *sep, const char *fmt), unsigned int offset=0, const char *linePrefix=0, const char *suffix="\r\n", const char *sep=",", const char *fmt=0) const
Definition yatemath.h:1107
SliceVector(const SliceVector &other, unsigned int offs, unsigned int len=0)
Definition yatemath.h:483
unsigned int available(unsigned int offs, int len=-1) const
Definition yatemath.h:596
Obj * data(unsigned int offs, unsigned int len, Obj *&eod)
Definition yatemath.h:545
virtual ~SliceVector()
Definition yatemath.h:491
virtual unsigned int vectorSize() const
Definition yatemath.h:622
SliceVector(const SliceVector &v1, const SliceVector &v2)
Definition yatemath.h:450
String & dump(String &buf, String &(*func)(String &s, const Obj &o, const char *sep, const char *fmt), const char *sep=",", const char *fmt=0) const
Definition yatemath.h:1080
bool sum(const SliceVector &other)
Definition yatemath.h:835
bool setSlice(const SliceVector &other, unsigned int offs=0, unsigned int len=0)
Definition yatemath.h:677
static unsigned int size(unsigned int len)
Definition yatemath.h:1209
SliceVector & operator-=(const SliceVector &other)
Definition yatemath.h:1012
const Obj * data(unsigned int offs, unsigned int len) const
Definition yatemath.h:514
bool sub(const SliceVector &other)
Definition yatemath.h:860
SliceVector & operator*=(const Obj &value)
Definition yatemath.h:1046
bool copy(const SliceVector &src, unsigned int len, unsigned int offs=0, unsigned int srcOffs=0)
Definition yatemath.h:754
SliceVector tail(unsigned int len) const
Definition yatemath.h:703
SliceVector & operator-=(const Obj &value)
Definition yatemath.h:1024
String & dumpHex(String &buf, unsigned int lineLen, unsigned int offset=0, const char *linePrefix=0, const char *suffix="\r\n") const
Definition yatemath.h:1154
SliceVector(const SliceVector &other)
Definition yatemath.h:427
unsigned int size() const
Definition yatemath.h:585
unsigned int maxLen() const
Definition yatemath.h:578
Obj & operator[](signed int index)
Definition yatemath.h:942
const Obj * data(unsigned int offs=0) const
Definition yatemath.h:533
void apply(void(*func)(Obj &))
Definition yatemath.h:786
Obj sumApply(Obj(*func)(const Obj &)) const
Definition yatemath.h:809
void mul(const Obj &value)
Definition yatemath.h:899
SliceVector head(unsigned int len) const
Definition yatemath.h:686
String & hexify(String &buf, char sep=0) const
Definition yatemath.h:1140
void sum(const Obj &value)
Definition yatemath.h:849
SliceVector slice(unsigned int offs, unsigned int len) const
Definition yatemath.h:729
bool operator==(const SliceVector &other) const
Definition yatemath.h:964
bool operator!=(const SliceVector &other) const
Definition yatemath.h:972
SliceVector & operator*=(const SliceVector &other)
Definition yatemath.h:1034
void bzero(unsigned int offs, unsigned int len)
Definition yatemath.h:763
static unsigned int objSize()
Definition yatemath.h:1201
bool resize(unsigned int len)
Definition yatemath.h:631
SliceVector()
Definition yatemath.h:418
void sub(const Obj &value)
Definition yatemath.h:874
void fill(const Obj &value)
Definition yatemath.h:776
SliceVector(unsigned int len, const Obj *buf=0, unsigned int maxLen=0)
Definition yatemath.h:439
Obj * data(unsigned int offs=0)
Definition yatemath.h:525
const Obj & operator[](unsigned int index) const
Definition yatemath.h:931
void mul(float value)
Definition yatemath.h:909
bool tail(SliceVector &dest, unsigned int len) const
Definition yatemath.h:715
SliceVector & operator*=(float value)
Definition yatemath.h:1056
int unHexify(const String &str, int sep=255)
Definition yatemath.h:1194
SliceVector & operator+=(const Obj &value)
Definition yatemath.h:1002
bool equals(const SliceVector &other) const
Definition yatemath.h:1066
Obj sum() const
Definition yatemath.h:796
bool slice(SliceVector &dest, unsigned int offs, unsigned int len=0) const
Definition yatemath.h:740
void steal(SliceVector &other)
Definition yatemath.h:649
SliceVector(const SliceVector &v1, const SliceVector &v2, const SliceVector &v3)
Definition yatemath.h:465
Obj * data(unsigned int offs, unsigned int len)
Definition yatemath.h:501
const Obj * data(unsigned int offs, unsigned int len, const Obj *&eod) const
Definition yatemath.h:560
unsigned int length() const
Definition yatemath.h:570
unsigned int availableClamp(unsigned int clamp, unsigned int offs=0, int len=-1) const
Definition yatemath.h:612
bool head(SliceVector &dest, unsigned int len) const
Definition yatemath.h:695
void bzero()
Definition yatemath.h:769
const Obj & operator[](signed int index) const
Definition yatemath.h:953
float sumApplyF(float(*func)(const Obj &)) const
Definition yatemath.h:822
int unHexify(const char *str, unsigned int len, int sep=255)
Definition yatemath.h:1176
bool mul(const SliceVector &other)
Definition yatemath.h:885
SliceVector & operator=(const SliceVector &other)
Definition yatemath.h:980
void resetStorage(unsigned int len, unsigned int maxLen=0)
Definition yatemath.h:665
SliceVector & operator+=(const SliceVector &other)
Definition yatemath.h:990
Obj & operator[](unsigned int index)
Definition yatemath.h:920
Definition yatemime.h:34
Complex operator+(const Complex &c1, const Complex &c2)
Definition yatemath.h:1567
Complex operator-(const Complex &c1, const Complex &c2)
Definition yatemath.h:1602
Complex operator/(const Complex &c1, const Complex &c2)
Definition yatemath.h:1661
String & operator<<(String &str, const Complex &c)
Definition yatemath.h:1685
Complex operator*(const Complex &c1, const Complex &c2)
Definition yatemath.h:1626