scifir-units 2.0.0
scifir-units is a library of units of measurement, angles, coordinates, fields, and related data, all lightweight, that help in the development of scientific software and scientific machines
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
scalar_unit.cpp
Go to the documentation of this file.
1#include "./scalar_unit.hpp"
2
3#include "./conversion.hpp"
4#include "./base_units.hpp"
5#include "./prefix.hpp"
6#include "../util/types.hpp"
7
8#include "boost/algorithm/string/erase.hpp"
9#include "boost/algorithm/string.hpp"
10
11#include <algorithm>
12#include <cctype>
13#include <cmath>
14#include <iostream>
15#include <map>
16#include <sstream>
17#include <string>
18
19using namespace std;
20
21namespace scifir
22{
23 scalar_unit::scalar_unit() : dimensions(),value(0.0f)
24 {}
25
26 scalar_unit::scalar_unit(const scalar_unit& x) : dimensions(x.get_dimensions()),value(x.get_value())
27 {}
28
29 scalar_unit::scalar_unit(scalar_unit&& x) : dimensions(std::move(x.get_dimensions())),value(std::move(x.get_value()))
30 {}
31
34
37
40
43
46
49
51 {}
52
55
58
61
64
67
72
74 {
75 if (has_dimensions(x))
76 {
78 value = x.get_value();
79 }
80 else
81 {
82 cerr << "Cannot initialize to different dimensions" << endl;
83 }
84 return *this;
85 }
86
88 {
89 if (has_dimensions(x))
90 {
91 dimensions = std::move(x.get_dimensions());
92 value = std::move(x.get_value());
93 }
94 else
95 {
96 cerr << "Cannot initialize to different dimensions" << endl;
97 }
98 return *this;
99 }
100
106
107 scalar_unit::operator float() const
108 {
109 return value;
110 }
111
113 {
114 x.change_dimensions(*this);
115 if(get_value() == x.get_value() and has_dimensions(x))
116 {
117 return true;
118 }
119 else
120 {
121 return false;
122 }
123 }
124
126 {
127 if(has_dimensions(x))
128 {
129 x.change_dimensions(*this);
130 scalar_unit a = *this;
131 a += x.get_value();
132 return a;
133 }
134 else
135 {
136 cerr << "Cannot sum different dimensions" << endl;
137 return scalar_unit();
138 }
139 }
140
142 {
143 if(has_dimensions(x))
144 {
145 x.change_dimensions(*this);
146 scalar_unit a = *this;
147 a -= x.get_value();
148 return a;
149 }
150 else
151 {
152 cerr << "Cannot substract different dimensions" << endl;
153 return scalar_unit();
154 }
155 }
156
164
172
174 {
176 {
177 return scalar_unit(std::pow(value,x.get_value()),power_dimensions(get_dimensions(),int(x.get_value())));
178 }
179 else
180 {
181 cerr << "Exponent with dimensions doesn't exist" << endl;
182 return scalar_unit();
183 }
184 }
185
187 {
188 if(!has_dimensions(x))
189 {
190 cerr << "Cannot sum different dimensions" << endl;
191 return;
192 }
193 x.change_dimensions(*this);
194 value += x.get_value();
195 }
196
198 {
199 if(!has_dimensions(x))
200 {
201 cerr << "Cannot substract different dimensions" << endl;
202 return;
203 }
204 x.change_dimensions(*this);
205 value -= x.get_value();
206 }
207
209 {
210 value++;
211 return *this;
212 }
213
215 {
216 scalar_unit& tmp(*this);
217 operator ++();
218 return tmp;
219 }
220
222 {
223 value--;
224 return *this;
225 }
226
228 {
229 scalar_unit& tmp(*this);
230 operator --();
231 return tmp;
232 }
233
238 {
240 {
241 return;
242 }
244 {
246 if(actual_dimension.is_composite_dimension())
247 {
248 vector<dimension> base_dimensions = actual_dimension.get_base_dimensions();
249 for(const dimension& base_dimension : base_dimensions)
250 {
252 }
253 }
254 }
255 if (dimensions.size() == 1)
256 {
257 if (dimensions[0].dimension_type == dimension::CELSIUS and dimensions[0].dimension_position == dimension::NUMERATOR and new_dimensions[0].dimension_type == dimension::KELVIN and new_dimensions[0].dimension_position == dimension::NUMERATOR)
258 {
259 value += 273.15f;
260 }
261 else if (dimensions[0].dimension_type == dimension::KELVIN and dimensions[0].dimension_position == dimension::NUMERATOR and new_dimensions[0].dimension_type == dimension::CELSIUS and new_dimensions[0].dimension_position == dimension::NUMERATOR)
262 {
263 value -= 273.15f;
264 }
265 }
266 dimensions.clear();
268 {
270 if(new_dimension.is_composite_dimension())
271 {
274 {
276 }
277 }
278 }
280 }
281 else
282 {
283 cerr << "Cannot change to different base dimensions" << endl;
284 }
285 }
286
288 {
290 {
292 {
293 return;
294 }
296 {
298 if(actual_dimension.is_composite_dimension())
299 {
300 vector<dimension> base_dimensions = actual_dimension.get_base_dimensions();
301 for(const dimension& base_dimension : base_dimensions)
302 {
304 }
305 }
306 }
307 if (dimensions.size() == 1 and x.get_dimensions().size() == 1)
308 {
309 if (dimensions[0].dimension_type == dimension::CELSIUS and dimensions[0].dimension_position == dimension::NUMERATOR and x.get_dimensions()[0].dimension_type == dimension::KELVIN and x.get_dimensions()[0].dimension_position == dimension::NUMERATOR)
310 {
311 value += 273.15f;
312 }
313 else if (dimensions[0].dimension_type == dimension::KELVIN and dimensions[0].dimension_position == dimension::NUMERATOR and x.get_dimensions()[0].dimension_type == dimension::CELSIUS and x.get_dimensions()[0].dimension_position == dimension::NUMERATOR)
314 {
315 value -= 273.15f;
316 }
317 }
318 dimensions.clear();
319 for(const dimension& new_dimension : x.get_dimensions())
320 {
322 if(new_dimension.is_composite_dimension())
323 {
326 {
328 }
329 }
330 }
332 }
333 else
334 {
335 cerr << "Cannot change to different base dimensions" << endl;
336 }
337 }
338
344
349
351 {
353 }
354
356 {
357 if(dimensions.size() == 0)
358 {
359 return true;
360 }
361 else
362 {
363 return false;
364 }
365 }
366
368 {
370 {
371 return true;
372 }
373 else
374 {
375 for (const dimension& x : dimensions)
376 {
377 if (!x.is_dimensionless())
378 {
379 return false;
380 }
381 }
382 return true;
383 }
384 }
385
387 {
388 if (dimensions.size() == 1)
389 {
390 return dimensions[0].is_simple_dimension();
391 }
392 else
393 {
394 return false;
395 }
396 }
397
399 {
400 return (dimensions.size() == 1);
401 }
402
404 {
405 if (dimensions.size() == 0)
406 {
407 return false;
408 }
409 else if (dimensions.size() == 1)
410 {
411 return dimensions[0].is_composite_dimension();
412 }
413 else
414 {
415 return true;
416 }
417 }
418
420 {
421 return to_string(dimensions);
422 }
423
424 /*string scalar_unit::get_dimensions_match() const
425 {
426 return "";
427 }*/
428
430 {
431 if (dimensions.size() == 1)
432 {
433 return dimensions[0].dimension_type;
434 }
435 else
436 {
437 return dimension::NONE;
438 }
439 }
440
445
447 {
449 if (dimensions.size() == 1 and use_close_prefix == true)
450 {
452 if (get_value() != 0)
453 {
454 int value_scale = int(log10(get_value()));
456 }
457 else
458 {
459 display_prefix = dimensions[0].prefix;
460 }
461 long double x_value = get_value();
462 x_value *= dimensions[0].prefix_math();
463 x_value /= dimensions[0].prefix_math(display_prefix);
465 x_dimensions[0].prefix = display_prefix;
467 }
468 else
469 {
471 }
472 return output.str();
473 }
474
476 {
478 long double x_value = get_value();
480 if (base_dimensions.size() == 1 and use_close_prefix == true)
481 {
482 int value_scale = int(log10(get_value()));
484 x_value *= base_dimensions[0].prefix_math();
485 x_value /= base_dimensions[0].prefix_math(display_prefix);
486 vector<dimension> x_dimensions = base_dimensions;
487 x_dimensions[0].prefix = display_prefix;
488 output << display_float(float(x_value),number_of_decimals) << " " << to_string(base_dimensions,with_brackets);
489 }
490 else
491 {
492 output << display_float(float(x_value),number_of_decimals) << " " << to_string(base_dimensions,with_brackets);
493 }
494 return output.str();
495 }
496
498 {
500 long double new_value = get_value();
501 if (init_dimensions != "sci")
502 {
503 for(const dimension& x_dimension : dimensions)
504 {
505 if (x_dimension.dimension_position == dimension::NUMERATOR)
506 {
507 new_value *= x_dimension.get_conversion_factor();
508 new_value *= x_dimension.prefix_math();
509 }
510 else if (x_dimension.dimension_position == dimension::DENOMINATOR)
511 {
512 new_value /= x_dimension.get_conversion_factor();
513 new_value /= x_dimension.prefix_math();
514 }
515 }
517 for(const dimension& x_dimension : base_dimensions)
518 {
519 if (x_dimension.dimension_position == dimension::NUMERATOR)
520 {
521 new_value *= x_dimension.prefix_math();
522 }
523 else if (x_dimension.dimension_position == dimension::DENOMINATOR)
524 {
525 new_value /= x_dimension.prefix_math();
526 }
527 }
529 if (dimensions.size() == 1 and new_dimensions.size() == 1)
530 {
531 if (dimensions[0].dimension_type == dimension::CELSIUS and dimensions[0].dimension_position == dimension::NUMERATOR and new_dimensions[0].dimension_type == dimension::KELVIN and new_dimensions[0].dimension_position == dimension::NUMERATOR)
532 {
533 new_value += 273.15l;
534 }
535 else if (dimensions[0].dimension_type == dimension::KELVIN and dimensions[0].dimension_position == dimension::NUMERATOR and new_dimensions[0].dimension_type == dimension::CELSIUS and new_dimensions[0].dimension_position == dimension::NUMERATOR)
536 {
537 new_value -= 273.15l;
538 }
539 }
541 {
542 if (x_new_dimension.dimension_position == dimension::NUMERATOR)
543 {
544 new_value /= x_new_dimension.get_conversion_factor();
545 new_value /= x_new_dimension.prefix_math();
546 }
547 else if (x_new_dimension.dimension_position == dimension::DENOMINATOR)
548 {
549 new_value *= x_new_dimension.get_conversion_factor();
550 new_value *= x_new_dimension.prefix_math();
551 }
552 }
554 if (with_brackets)
555 {
556 output << "[";
557 }
559 if (with_brackets)
560 {
561 output << "]";
562 }
563 }
564 else
565 {
566 for (const dimension& x_dimension : dimensions)
567 {
568 new_value *= x_dimension.prefix_math();
569 }
572 {
573 x_new_dimension.prefix.prefix_type = prefix::NONE;
574 }
575 int value_scale = int(log10(get_value()));
577 }
578 return output.str();
579 }
580
582 {
584 long double new_value = get_value();
585 if (init_dimensions != "sci")
586 {
588 if (init_dimensions != "")
589 {
590 for(const dimension& x_dimension : dimensions)
591 {
592 if (x_dimension.dimension_position == dimension::NUMERATOR)
593 {
594 new_value *= x_dimension.get_conversion_factor();
595 new_value *= x_dimension.prefix_math();
596 }
597 else if (x_dimension.dimension_position == dimension::DENOMINATOR)
598 {
599 new_value /= x_dimension.get_conversion_factor();
600 new_value /= x_dimension.prefix_math();
601 }
602 }
604 for(const dimension& x_dimension : base_dimensions)
605 {
606 if (x_dimension.dimension_position == dimension::NUMERATOR)
607 {
608 new_value *= x_dimension.prefix_math();
609 }
610 else if (x_dimension.dimension_position == dimension::DENOMINATOR)
611 {
612 new_value /= x_dimension.prefix_math();
613 }
614 }
616 if (dimensions.size() == 1 and new_dimensions.size() == 1)
617 {
618 if (dimensions[0].dimension_type == dimension::CELSIUS and dimensions[0].dimension_position == dimension::NUMERATOR and new_dimensions[0].dimension_type == dimension::KELVIN and new_dimensions[0].dimension_position == dimension::NUMERATOR)
619 {
620 new_value += 273.15l;
621 }
622 else if (dimensions[0].dimension_type == dimension::KELVIN and dimensions[0].dimension_position == dimension::NUMERATOR and new_dimensions[0].dimension_type == dimension::CELSIUS and new_dimensions[0].dimension_position == dimension::NUMERATOR)
623 {
624 new_value -= 273.15l;
625 }
626 }
628 {
629 if (x_new_dimension.dimension_position == dimension::NUMERATOR)
630 {
631 new_value /= x_new_dimension.get_conversion_factor();
632 new_value /= x_new_dimension.prefix_math();
633 }
634 else if (x_new_dimension.dimension_position == dimension::DENOMINATOR)
635 {
636 new_value *= x_new_dimension.get_conversion_factor();
637 new_value *= x_new_dimension.prefix_math();
638 }
639 }
640 }
641 else
642 {
644 }
646 }
647 else
648 {
649 for (const dimension& x_dimension : dimensions)
650 {
651 new_value *= x_dimension.prefix_math();
652 }
655 {
656 x_new_dimension.prefix.prefix_type = prefix::NONE;
657 }
658 int value_scale = int(log10(get_value()));
660 }
661 return output.str();
662 }
663
665 {
666 if (new_dimension.dimension_position == dimension::NUMERATOR)
667 {
668 value /= float(new_dimension.get_conversion_factor());
669 value /= float(new_dimension.prefix_math());
670 }
671 else if (new_dimension.dimension_position == dimension::DENOMINATOR)
672 {
673 value *= float(new_dimension.get_conversion_factor());
674 value *= float(new_dimension.prefix_math());
675 }
676 }
677
679 {
680 if (old_dimension.dimension_position == dimension::NUMERATOR)
681 {
682 value *= float(old_dimension.get_conversion_factor());
683 value *= float(old_dimension.prefix_math());
684 }
685 else if (old_dimension.dimension_position == dimension::DENOMINATOR)
686 {
687 value /= float(old_dimension.get_conversion_factor());
688 value /= float(old_dimension.prefix_math());
689 }
690 }
691
693 {
694 if(!isdigit(init_scalar[0]))
695 {
696 return;
697 }
698 else
699 {
700 int i = 0;
701 while(isdigit(init_scalar[i]) || init_scalar[i] == '.' || init_scalar[i] == ' ' || init_scalar[i] == '*' || init_scalar[i] == '^' || init_scalar[i] == 'e' || init_scalar[i] == 'E')
702 {
703 if (init_scalar[i] == ' ')
704 {
705 break;
706 }
707 i++;
708 }
709 string string_value = init_scalar.substr(0, i);
710 boost::algorithm::erase_all(string_value, " ");
711 size_t search_e = string_value.find("E");
712 if (search_e != string::npos)
713 {
714 string_value.replace(search_e,1,"e");
715 }
716 size_t search_10 = string_value.find("*10^");
717 if (search_10 != string::npos)
718 {
719 string_value.replace(search_10,4,"e");
720 }
722 if (real_dimensions.size() == 0 or equal_dimensions(new_dimensions,real_dimensions))
723 {
725 ss >> value;
727 }
728 }
729 }
730
732 {
733 if (!has_dimensions(real_dimensions))
734 {
735 value = 0;
736 dimensions.clear();
737 }
738 }
739
740 string to_string(const scalar_unit& x)
741 {
742 return x.display(2);
743 }
744
745 bool is_scalar_unit(const string& init_scalar)
746 {
747 bool dot_present = false;
748 bool e_present = false;
749 unsigned int current_pos = 0;
750 int e_present_pos = 0;
751 for (unsigned int i = 0; i < init_scalar.length(); i++)
752 {
753 if (init_scalar[i] == ' ')
754 {
755 if (e_present and ((unsigned int)(e_present_pos + 1) == i))
756 {
757 return false;
758 }
759 current_pos = i;
760 break;
761 }
762 if (e_present == false)
763 {
764 if (init_scalar[i] == '.')
765 {
766 if (dot_present)
767 {
768 return false;
769 }
770 else
771 {
772 dot_present = true;
773 }
774 }
775 else if (init_scalar[i] == 'e' or init_scalar[i] == 'E')
776 {
777 e_present = true;
779 continue;
780 }
781 else if (init_scalar[i] == '*')
782 {
783 if (!(init_scalar.substr(i + 1,3) == "10^"))
784 {
785 return false;
786 }
787 else
788 {
789 e_present = true;
790 i += 3;
792 continue;
793 }
794 }
795 else if (!isdigit(init_scalar[i]))
796 {
797 return false;
798 }
799 else if (i == (init_scalar.length() - 1))
800 {
801 return false;
802 }
803 }
804 else
805 {
806 if (!isdigit(init_scalar[i]))
807 {
808 return false;
809 }
810 }
811 }
812 if (current_pos == (init_scalar.length() - 1))
813 {
814 return false;
815 }
816 if (current_pos == 0)
817 {
818 return false;
819 }
820 bool brackets_present = false;
821 if (init_scalar.at(current_pos + 1) == '[')
822 {
823 if (init_scalar.at(init_scalar.size() - 1) != ']')
824 {
825 return false;
826 }
827 current_pos++;
828 brackets_present = true;
829 }
830 vector<string> values;
831 boost::split(values,init_scalar.substr(current_pos + 1,init_scalar.size() - 1),boost::is_any_of("/"));
833 {
834 values[values.size() - 1] = values[values.size() - 1].substr(0,values[values.size() - 1].size() - 1);
835 }
836 if (values.size() == 1)
837 {
839 boost::split(subvalues,values[0],boost::is_any_of("*"));
840 for (string& x_subvalue : subvalues)
841 {
842 boost::trim(x_subvalue);
843 bool number_present = false;
844 for (unsigned int i = 0; i < x_subvalue.length(); i++)
845 {
846 if (number_present == false)
847 {
848 if (isdigit(x_subvalue[i]))
849 {
850 number_present = true;
851 continue;
852 }
853 else if (!isalpha(x_subvalue[i]))
854 {
855 return false;
856 }
857 }
858 else
859 {
860 if (!isdigit(x_subvalue[i]))
861 {
862 return false;
863 }
864 }
865 }
866 }
867 return true;
868 }
869 else if (values.size() == 2)
870 {
871 if (values[0] != "1")
872 {
874 boost::split(subvalues,values[0],boost::is_any_of("*"));
875 for (string& x_subvalue : subvalues)
876 {
877 boost::trim(x_subvalue);
878 bool number_present = false;
879 for (unsigned int i = 0; i < x_subvalue.length(); i++)
880 {
881 if (number_present == false)
882 {
883 if (isdigit(x_subvalue[i]))
884 {
885 number_present = true;
886 continue;
887 }
888 else if (!isalpha(x_subvalue[i]))
889 {
890 return false;
891 }
892 }
893 else
894 {
895 if (!isdigit(x_subvalue[i]))
896 {
897 return false;
898 }
899 }
900 }
901 }
902 }
904 boost::split(subvalues_denominator,values[1],boost::is_any_of("*"));
905 for (string& x_subvalue : subvalues_denominator)
906 {
907 boost::trim(x_subvalue);
908 bool number_present = false;
909 for (unsigned int i = 0; i < x_subvalue.length(); i++)
910 {
911 if (number_present == false)
912 {
913 if (isdigit(x_subvalue[i]))
914 {
915 number_present = true;
916 continue;
917 }
918 else if (!isalpha(x_subvalue[i]))
919 {
920 return false;
921 }
922 }
923 else
924 {
925 if (!isdigit(x_subvalue[i]))
926 {
927 return false;
928 }
929 }
930 }
931 }
932 return true;
933 }
934 return false;
935 }
936
937 float abs(const scalar_unit& x)
938 {
939 return std::abs(x.get_value());
940 }
941
943 {
944 return x ^ exponent;
945 }
946
954
955 scalar_unit sqrt_nth(const scalar_unit& x, int index)
956 {
957 long double new_value = x.get_value();
959 new_value = std::pow(new_value, 1.0f / index);
961 }
962}
963
965{
966 return !(x == y);
967}
968
970{
971 if(!x.has_dimensions(y))
972 {
973 return false;
974 }
977 if(z.get_value() < y.get_value())
978 {
979 return true;
980 }
981 else
982 {
983 return false;
984 }
985}
986
988{
989 if(!x.has_dimensions(y))
990 {
991 return false;
992 }
995 if(z.get_value() > y.get_value())
996 {
997 return true;
998 }
999 else
1000 {
1001 return false;
1002 }
1003}
1004
1006{
1007 return !(x > y);
1008}
1009
1011{
1012 return !(x < y);
1013}
1014
1015bool operator ==(const scifir::scalar_unit& x, const string& init_scalar)
1016{
1017 scifir::scalar_unit y(init_scalar);
1018 return (x == y);
1019}
1020
1021bool operator !=(const scifir::scalar_unit& x, const string& init_scalar)
1022{
1023 return !(x == init_scalar);
1024}
1025
1026bool operator <(const scifir::scalar_unit& x, const string& init_scalar)
1027{
1028 scifir::scalar_unit y(init_scalar);
1029 return (x < y);
1030}
1031
1032bool operator >(const scifir::scalar_unit& x, const string& init_scalar)
1033{
1034 scifir::scalar_unit y(init_scalar);
1035 return (x > y);
1036}
1037
1038bool operator <=(const scifir::scalar_unit& x, const string& init_scalar)
1039{
1040 return !(x > init_scalar);
1041}
1042
1043bool operator >=(const scifir::scalar_unit& x, const string& init_scalar)
1044{
1045 return !(x < init_scalar);
1046}
1047
1048bool operator ==(const string& init_scalar, const scifir::scalar_unit& x)
1049{
1050 return (x == init_scalar);
1051}
1052
1053bool operator !=(const string& init_scalar, const scifir::scalar_unit& x)
1054{
1055 return (x != init_scalar);
1056}
1057
1058bool operator <(const string& init_scalar, const scifir::scalar_unit& x)
1059{
1060 scifir::scalar_unit y(init_scalar);
1061 return (y < x);
1062}
1063
1064bool operator >(const string& init_scalar, const scifir::scalar_unit& x)
1065{
1066 scifir::scalar_unit y(init_scalar);
1067 return (y > x);
1068}
1069
1070bool operator <=(const string& init_scalar, const scifir::scalar_unit& x)
1071{
1072 return !(init_scalar > x);
1073}
1074
1075bool operator >=(const string& init_scalar, const scifir::scalar_unit& x)
1076{
1077 return !(init_scalar < x);
1078}
1079
1080void operator +=(string& x, const scifir::scalar_unit& y)
1081{
1082 ostringstream output;
1083 output << y;
1084 x += output.str();
1085}
1086
1087string operator +(const string& x, const scifir::scalar_unit& y)
1088{
1089 ostringstream output;
1090 output << x;
1091 output << y;
1092 return output.str();
1093}
1094
1095string operator +(const scifir::scalar_unit& y, const string& x)
1096{
1097 ostringstream output;
1098 output << y;
1099 output << x;
1100 return output.str();
1101}
1102
1103ostream& operator <<(ostream& os, const scifir::scalar_unit& x)
1104{
1105 return os << to_string(x);
1106}
1107
1108istream& operator >>(istream& is, scifir::scalar_unit& x)
1109{
1110 char a[256];
1111 is.getline(a, 256);
1112 string b(a);
1113 boost::trim(b);
1114 x = scifir::scalar_unit(b);
1115 return is;
1116}
Class that represents dimensions of the SI system of units. Each dimension sizes 6 bytes,...
Definition dimension.hpp:31
position
Represents the position of the dimension, which can be at the numerator or at the denominator....
Definition dimension.hpp:38
@ DENOMINATOR
The dimension is at the denominator.
Definition dimension.hpp:38
@ NUMERATOR
The dimension is at the numerator.
Definition dimension.hpp:38
type
Represents a dimension of the SI system of units. All the dimensions of the SI system of units are su...
Definition dimension.hpp:34
@ NONE
No dimension. Default value for the default constructor.
Definition dimension.hpp:35
@ CELSIUS
Celsius, plural celsius. SI dimension of temperature, derived from kelvin. Symbol °C....
Definition dimension.hpp:35
@ KELVIN
Kelvin, plural kelvins. SI dimension of temperature. Symbol K.
Definition dimension.hpp:35
Class that represents prefixes of the SI system of units. Each prefix sizes 1 byte....
Definition prefix.hpp:14
type
Represents a prefix of the SI system of units. All the prefixes of the SI system of units are support...
Definition prefix.hpp:16
@ NONE
There is no prefix. Then, the dimension is not increased or decreased by some factor.
Definition prefix.hpp:16
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions....
string to_latex(const string &init_dimensions, int number_of_decimals=2, bool with_brackets=false) const
bool has_single_dimensions() const
Returns true if there's only one dimension, which can be simple or composite.
bool has_empty_dimensions() const
Checks if there aren't base dimensions.
string display_dimensions() const
Generates an string of the dimensions of the scalar_unit, with the same format as the initialization ...
scalar_unit & operator--()
Decrement operator, it decreases the value by one.
bool has_composite_dimensions() const
Returns true is there's more than one simple dimension.
void add_dimension(const dimension &new_dimension)
Internal function. It adds a dimension, changing the value according to the conversion factor of the ...
vector< dimension > dimensions
Dimensions of the scalar_unit. They can be simple dimensions, composite dimensions or special names.
scalar_unit & operator=(const scalar_unit &x)
Copy assignment, it assigns a copy of the scalar_unit.
scalar_unit operator*(scalar_unit x) const
Multiplication operator, it multiplies two scalar_unit classes, their dimensions are also multiplied.
const vector< dimension > & get_dimensions() const
Read-only getter of the dimensions.
bool operator==(scalar_unit x) const
Comparison operator, two scalar_unit classes are considered equivalent if they have the same value gi...
scalar_unit operator/(scalar_unit x) const
Division operator, it divides one scalar_unit class with the other, their dimensions are also divided...
bool has_dimensions(const string &init_dimensions) const
Checks if the basic dimensions are the same as the initialization string of dimensions.
void check_dimensions(const vector< dimension > &real_dimensions)
dimension::type get_single_dimension_type() const
Returns the dimension::type if there's only one dimension, returns dimension::NONE if there's more th...
string display(int number_of_decimals=2, bool with_brackets=false, bool use_close_prefix=false) const
Generates a string representation of the scalar_unit, with the value and the dimensions....
vector< dimension > get_base_dimensions() const
Generates a set of the base dimensions of the dimensions of the scalar_unit.
float value
Value of the scalar_unit. It changes automatically when the dimensions change.
bool is_dimensionless() const
Returns true if there aren't dimensions or if all dimensions are dimensionless.
scalar_unit operator+(scalar_unit x) const
Addition operator, it sums two scalar_unit classes, their dimensions are changed to be equal first....
scalar_unit & operator++()
Increment operator, it increases the value by one.
bool has_simple_dimensions() const
Returns true if there's only a simple dimension.
scalar_unit operator-(scalar_unit x) const
Substraction operator, it substracts one scalar_unit from the other, their dimensions are changed to ...
void change_dimensions(const string &init_dimensions)
Changes the dimensions to the dimensions specified by the initialization string of dimensions.
scalar_unit()
Default constructor, the value is 0 and the dimensions are empty.
void operator+=(scalar_unit x)
Addition operator, it adds a scalar_unit class to another, by converting their dimensions to be equal...
void initialize_from_string(string init_scalar, const vector< dimension > &real_dimensions)
Internal function. It sets the value and the dimensions of the scalar_unit to the value and dimension...
string custom_display(const string &init_dimensions, int number_of_decimals=2, bool with_brackets=false) const
Generates a string representation of the scalar_unit, with the dimensions changed to any set of dimen...
void operator-=(scalar_unit x)
Substraction operator, it substracts a scalar_unit class to another, by converting their dimensions t...
string base_display(int number_of_decimals=2, bool with_brackets=false, bool use_close_prefix=false) const
Generates a string representation of the scalar_unit, with its dimensions converted to their base cou...
void remove_dimension(const dimension &old_dimension)
Internal function. It removes a dimension, changing the value according to the conversion factor of t...
const float & get_value() const
Read-only getter of the value.
scalar_unit operator^(const scalar_unit &x) const
Power operator, it powers a scalar_unit class with another, if that second scalar_unit class,...
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
angle sqrt_nth(const angle &x, int index)
Calculates the nth root of the angle x and returns that new angle.
Definition angle.cpp:421
T abs(const complex_number< T > &x)
bool equal_dimensions(const string &init_dimensions_x, const string &init_dimensions_y)
Checks if two initialization strings of dimensions initialize the same basic dimensions.
vector< dimension > divide_dimensions(vector< dimension > x, const vector< dimension > &y, long double &value)
Divides the first vector of dimensions with the other. The result is normalized after,...
bool equal_dimensions_and_prefixes(const vector< dimension > &x, const vector< dimension > &y)
Checks if the base dimensions of two vectors of dimensions are equal, and if they have also the same ...
bool is_scalar_unit(const string &init_scalar)
Checks if an string is an initialization string of a scalar_unit.
prefix closest_prefix(const prefix &actual_prefix, int actual_scale)
Returns the closes prefix related to the scale of the current value. It is used when displaying a sca...
Definition prefix.cpp:323
scalar_unit pow(const scalar_unit &x, int exponent)
Exponentiates a scalar_unit to some numeric type, the dimensions are also exponentiated.
string to_string(const aid &x)
Creates a string representation of aid, it's for aid equivalent to the display() function of aid.
Definition aid.cpp:582
string display_float(const float &value, int number_of_decimals)
Definition types.cpp:36
vector< dimension > create_base_dimensions(const string &init_dimensions)
Creates the base dimensions from an initialization string of dimensions.
string to_latex(const vector< dimension > &x_dimensions, bool with_brackets)
vector< dimension > multiply_dimensions(const vector< dimension > &x, const vector< dimension > &y)
Multiplies two vectors of dimensions. The result is normalized after, which means that equal dimensio...
vector< dimension > square_dimensions(vector< dimension > x, int index, long double &value)
Squares a vector of dimensions by an index. The value is updated too related to the prefix math and t...
vector< dimension > create_dimensions(string init_dimensions)
Creates the dimensions from an initialization string of dimensions.
angle sqrt(const angle &x)
Calculates the square root of the angle x and returns that new angle.
Definition angle.cpp:416
vector< dimension > power_dimensions(const vector< dimension > &x, int exponent)
Powers the dimensions by an exponent.
bool operator<=(const scifir::scalar_unit &x, const scifir::scalar_unit &y)
Returns true if x has a lower or equal value than y, compared with the same dimensions....
istream & operator>>(istream &is, scifir::scalar_unit &x)
Allows that an istream initializes by string a scalar_unit x.
bool operator>(const scifir::scalar_unit &x, const scifir::scalar_unit &y)
Returns true if x has a greather value than y, compared with the same dimensions. If their dimensions...
string operator+(const string &x, const scifir::scalar_unit &y)
Creates a new string as the concatenation of the string x with the representation string of the scala...
bool operator!=(const scifir::scalar_unit &x, scifir::scalar_unit y)
Returns true if two scalar_unit classes doesn't have the same value when changed to same dimensions....
ostream & operator<<(ostream &os, const scifir::scalar_unit &x)
Adds the string representation of the scalar_unit x to an output stream os.
void operator+=(string &x, const scifir::scalar_unit &y)
Concatenates the string representation of the scalar_unit y to the string x.
bool operator==(const scifir::scalar_unit &x, const string &init_scalar)
Returns true if x is equal to the scalar_unit initialized with the string being compared....
bool operator<(const scifir::scalar_unit &x, const scifir::scalar_unit &y)
Returns true if x has a lower value than y, compared with the same dimensions. If their dimensions ar...
bool operator>=(const scifir::scalar_unit &x, const scifir::scalar_unit &y)
Returns true if x has a greather or equal value than y, compared with the same dimensions....