26 template<jit::
float_scalar T,
bool SAFE_MATH=false>
70 if (ac->is(0) || ac->is(1)) {
85 ap2->get_num_columns(),
95 if (am->get_left()->is_constant()) {
97 return sqrt(am->get_left()) *
98 sqrt(am->get_right());
100 if (!am->get_left()->evaluate().is_negative()) {
101 return sqrt(am->get_left()) *
102 sqrt(am->get_right());
107 auto amlp =
pow_cast(am->get_left());
108 auto amrp =
pow_cast(am->get_right());
111 if (amlprc.get() && !amlprc->is(2)) {
112 return sqrt(am->get_left()) *
113 sqrt(am->get_right());
115 }
else if (amrp.get()) {
117 if (amrprc.get() && !amrprc->is(2)) {
118 return sqrt(am->get_left()) *
119 sqrt(am->get_right());
128 if (alm.get() && alm->get_left()->is_constant()) {
129 return sqrt(alm->get_left()) *
130 sqrt(alm->get_right()/ad->get_right());
135 if (
pow_cast(ad->get_left()).get() ||
136 ad->get_left()->is_constant() ||
138 ad->get_right()->is_constant()) {
139 return sqrt(ad->get_left()) /
140 sqrt(ad->get_right());
144 return this->shared_from_this();
160 const size_t hash =
reinterpret_cast<size_t> (x.get());
161 if (this->
df_cache.find(
hash) == this->df_cache.end()) {
163 / (2.0*this->shared_from_this());
182 if (registers.find(
this) == registers.end()) {
183 auto a = this->
arg->compile(stream, registers,
189 stream <<
" " << registers[
this] <<
" = sqrt("
190 << registers[a.get()] <<
")";
194 return this->shared_from_this();
204 if (
this == x.get()) {
210 return this->
arg->is_match(x_cast->get_arg());
220 std::cout <<
"\\sqrt{";
221 this->
arg->to_latex();
259 return sqrt(this->
arg->remove_pseudo());
261 return this->shared_from_this();
273 if (registers.find(
this) == registers.end()) {
275 registers[
this] = name;
276 stream <<
" " << name
277 <<
" [label = \"sqrt\", shape = oval, style = filled, fillcolor = blue, fontcolor = white];" << std::endl;
279 auto a = this->
arg->to_vizgraph(stream, registers);
280 stream <<
" " << name <<
" -- " << registers[a.get()] <<
";" << std::endl;
283 return this->shared_from_this();
296 template<jit::
float_scalar T,
bool SAFE_MATH=false>
298 auto temp = std::make_shared<sqrt_node<T, SAFE_MATH>> (x)->reduce();
300 for (
size_t i = temp->get_hash();
301 i < std::numeric_limits<size_t>::max();
i++) {
310#if defined(__clang__) || defined(__GNUC__)
311 __builtin_unreachable();
313 assert(
false &&
"Should never reach.");
318 template<jit::
float_scalar T,
bool SAFE_MATH=false>
330 template<jit::
float_scalar T,
bool SAFE_MATH=false>
332 return std::dynamic_pointer_cast<sqrt_node<T, SAFE_MATH>> (x);
346 template<jit::
float_scalar T,
bool SAFE_MATH=false>
400 ap2->get_num_columns(),
411 return this->shared_from_this();
427 const size_t hash =
reinterpret_cast<size_t> (x.get());
428 if (this->
df_cache.find(
hash) == this->df_cache.end()) {
448 if (registers.find(
this) == registers.end()) {
449 auto a = this->
arg->compile(stream, registers,
455 stream <<
" " << registers[
this] <<
" = ";
456 if constexpr (SAFE_MATH) {
460 stream << registers[a.get()];
464 stream <<
" < 709.8 ? ";
466 stream <<
"exp(" << registers[a.get()] <<
")";
467 if constexpr (SAFE_MATH) {
473 stream << jit::max_base<T> ();
482 return this->shared_from_this();
492 if (
this == x.get()) {
498 return this->
arg->is_match(x_cast->get_arg());
508 std::cout <<
"e^{\\left(";
509 this->
arg->to_latex();
510 std::cout <<
"\\right)}";
520 return exp(this->
arg->remove_pseudo());
522 return this->shared_from_this();
534 if (registers.find(
this) == registers.end()) {
536 registers[
this] = name;
537 stream <<
" " << name
538 <<
" [label = \"exp\", shape = oval, style = filled, fillcolor = blue, fontcolor = white];" << std::endl;
540 auto a = this->
arg->to_vizgraph(stream, registers);
541 stream <<
" " << name <<
" -- " << registers[a.get()] <<
";" << std::endl;
544 return this->shared_from_this();
557 template<jit::
float_scalar T,
bool SAFE_MATH=false>
559 auto temp = std::make_shared<exp_node<T, SAFE_MATH>> (x)->reduce();
561 for (
size_t i = temp->get_hash();
562 i < std::numeric_limits<size_t>::max();
i++) {
571#if defined(__clang__) || defined(__GNUC__)
572 __builtin_unreachable();
574 assert(
false &&
"Should never reach.");
579 template<jit::
float_scalar T,
bool SAFE_MATH=false>
591 template<jit::
float_scalar T,
bool SAFE_MATH=false>
593 return std::dynamic_pointer_cast<exp_node<T, SAFE_MATH>> (x);
607 template<jit::
float_scalar T,
bool SAFE_MATH=false>
661 ap2->get_num_columns(),
672 return this->shared_from_this();
688 const size_t hash =
reinterpret_cast<size_t> (x.get());
689 if (this->
df_cache.find(
hash) == this->df_cache.end()) {
709 if (registers.find(
this) == registers.end()) {
710 auto a = this->
arg->compile(stream, registers,
716 stream <<
" " << registers[
this] <<
" = log("
717 << registers[a.get()] <<
")";
721 return this->shared_from_this();
731 if (
this == x.get()) {
737 return this->
arg->is_match(x_cast->get_arg());
747 std::cout <<
"\\ln{\\left(";
748 this->
arg->to_latex();
749 std::cout <<
"\\right)}";
759 return log(this->
arg->remove_pseudo());
761 return this->shared_from_this();
773 if (registers.find(
this) == registers.end()) {
775 registers[
this] = name;
776 stream <<
" " << name
777 <<
" [label = \"log\", shape = oval, style = filled, fillcolor = blue, fontcolor = white];" << std::endl;
779 auto a = this->
arg->to_vizgraph(stream, registers);
780 stream <<
" " << name <<
" -- " << registers[a.get()] <<
";" << std::endl;
783 return this->shared_from_this();
796 template<jit::
float_scalar T,
bool SAFE_MATH=false>
798 auto temp = std::make_shared<log_node<T, SAFE_MATH>> (x)->reduce();
800 for (
size_t i = temp->get_hash();
i < std::numeric_limits<size_t>::max();
i++) {
809#if defined(__clang__) || defined(__GNUC__)
810 __builtin_unreachable();
812 assert(
false &&
"Should never reach.");
817 template<jit::
float_scalar T,
bool SAFE_MATH=false>
829 template<jit::
float_scalar T,
bool SAFE_MATH=false>
831 return std::dynamic_pointer_cast<log_node<T, SAFE_MATH>> (x);
845 template<jit::
float_scalar T,
bool SAFE_MATH=false>
895 if (rc.get() && rc->is(0)) {
897 }
else if (rc.get() && rc->is(1)) {
899 }
else if (rc.get() && rc->is(0.5)) {
901 }
else if (rc.get() && rc->is(2)){
904 return sq->get_arg();
908 if (lc.get() && rc.get()) {
914 if (pl1.get() && (rc.get() || pl1->is_arg_match(this->right))) {
916 }
else if (pr1.get() && (lc.get() || pr1->is_arg_match(this->left))) {
922 if (pl2.get() && (rc.get() || pl2->is_arg_match(this->right))) {
924 pl2->get_num_columns(),
927 }
else if (pr2.get() && (lc.get() || pr2->is_arg_match(this->left))) {
929 pr2->get_num_columns(),
935 if (pr2.get() && pr2->is_row_match(this->left)) {
937 result.
pow_row(pr2->evaluate());
939 pr2->get_num_columns(),
942 }
else if (pr2.get() && pr2->is_col_match(this->left)) {
944 result.
pow_col(pr2->evaluate());
946 pr2->get_num_columns(),
949 }
else if (pl2.get() && pl2->is_row_match(this->right)) {
951 result.
pow_row(pr1->evaluate());
953 pl2->get_num_columns(),
956 }
else if (pl2.get() && pl2->is_col_match(this->right)) {
958 result.
pow_col(pr1->evaluate());
960 pl2->get_num_columns(),
967 if (lp.get() && rc.get() && rc->is_integer()) {
968 return pow(lp->get_left(), lp->get_right()*this->right);
974 if (lm.get() && rc.get()) {
975 if (lm->get_left()->is_constant() ||
976 lm->get_right()->is_constant() ||
981 return pow(lm->get_left(), this->right) *
982 pow(lm->get_right(), this->right);
989 if (lmlm->get_left()->is_constant() ||
990 lmlm->get_right()->is_constant() ||
994 pow_cast(lmlm->get_right()).get()) {
995 return pow(lmlm->get_left(), this->right) *
996 pow(lmlm->get_right(), this->right) *
997 pow(lm->get_right(), this->right);
1004 if (ld.get() && rc.get()) {
1010 rc->evaluate().is_even()) {
1011 if (ldlm->get_left()->is_constant()) {
1012 return pow(ldlm->get_left(), this->right) *
1013 pow(ldlm->get_right()/ld->get_right(),
1017 if (ldlm->get_left()->is_constant() ||
1018 ldlm->get_right()->is_constant() ||
1021 pow_cast(ldlm->get_left()).get() ||
1022 pow_cast(ldlm->get_right()).get()) {
1023 return pow(ldlm->get_left(), this->right) *
1024 pow(ldlm->get_right(), this->right)/
1025 pow(ld->get_right(), this->right);
1030 if (ldlmlm->get_left()->is_constant() ||
1031 ldlmlm->get_right()->is_constant() ||
1034 pow_cast(ldlmlm->get_left()).get() ||
1035 pow_cast(ldlmlm->get_right()).get()) {
1036 return (
pow(ldlmlm->get_left(), this->right) *
1037 pow(ldlmlm->get_right(), this->right) *
1038 pow(ldlm->get_right(), this->right)) /
1039 pow(ld->get_right(), this->right);
1045 if (ldlmrm->get_left()->is_constant() ||
1046 ldlmrm->get_right()->is_constant() ||
1049 pow_cast(ldlmrm->get_left()).get() ||
1050 pow_cast(ldlmrm->get_right()).get()) {
1051 return (
pow(ldlmrm->get_left(), this->right) *
1052 pow(ldlmrm->get_right(), this->right) *
1053 pow(ldlm->get_left(), this->right)) /
1054 pow(ld->get_right(), this->right);
1060 if (ld->get_left()->is_constant() ||
1061 ld->get_right()->is_constant() ||
1066 return pow(ld->get_left(), this->right) /
1067 pow(ld->get_right(), this->right);
1073 if (ldrm->get_left()->is_constant() ||
1074 ldrm->get_right()->is_constant() ||
1077 pow_cast(ldrm->get_left()).get() ||
1078 pow_cast(ldrm->get_right()).get()) {
1079 return pow(ld->get_left(), this->right) /
1080 (
pow(ldrm->get_left(), this->right) *
1081 pow(ldrm->get_right(), this->right));
1086 if (ldrmlm->get_left()->is_constant() ||
1087 ldrmlm->get_right()->is_constant() ||
1090 pow_cast(ldrmlm->get_left()).get() ||
1091 pow_cast(ldrmlm->get_right()).get()) {
1092 return pow(ld->get_left(), this->right) /
1093 (
pow(ldrmlm->get_left(), this->right) *
1094 pow(ldrmlm->get_right(), this->right) *
1095 pow(ldrm->get_right(), this->right));
1101 if (ldrmrm->get_left()->is_constant() ||
1102 ldrmrm->get_right()->is_constant() ||
1105 pow_cast(ldrmrm->get_left()).get() ||
1106 pow_cast(ldrmrm->get_right()).get()) {
1107 return pow(ld->get_left(), this->right) /
1108 (
pow(ldrmrm->get_left(), this->right) *
1109 pow(ldrmrm->get_right(), this->right) *
1110 pow(ldrm->get_left(), this->right));
1117 return pow(ld->get_left()->get_power_base(),
1118 this->right*(ld->get_left()->get_power_exponent() -
1119 ld->get_right()->get_power_exponent()));
1126 ldrmlm->get_right()->get_power_base())) {
1127 return pow(ld->get_left()/ldrmlm->get_left(),
1129 pow(ldrm->get_right()*ldrmlm->get_right(),
1132 ldrmlm->get_left()->get_power_base())) {
1133 return pow(ld->get_left()/ldrmlm->get_right(),
1135 pow(ldrm->get_right()*ldrmlm->get_left(),
1138 ldrmlm->get_right()->get_power_base()) ||
1140 ldrmlm->get_left()->get_power_base())) {
1141 return pow(ld->get_left()/ldrm->get_right(),
1143 pow(ldrmlm->get_left()*ldrmlm->get_right(),
1153 return pow(lsq->get_arg(),
1159 if (temp.get() && rc.get() && rc->is_integer()) {
1160 return exp(this->
right*temp->get_arg());
1163 if constexpr (std::floating_point<T>) {
1166 if (lhp.get() && rc.get() && rc->is(2)) {
1167 return pow(lhp->get_left(), this->right) +
1168 pow(lhp->get_right(), this->right);
1173 if (ld.get() && rc.get() && rc->is(2)) {
1177 return (
pow(ldlhp->get_left(),
static_cast<T
> (2)) +
1178 pow(ldlhp->get_right(),
static_cast<T
> (2))) /
1179 pow(ld->get_right(),
static_cast<T
> (2));
1180 }
else if (ldrhp.get()) {
1181 return pow(ld->get_left(),
static_cast<T
> (2)) /
1182 (
pow(ldrhp->get_left(),
static_cast<T
> (2)) +
1183 pow(ldrhp->get_right(),
static_cast<T
> (2)));
1188 return this->shared_from_this();
1205 const size_t hash =
reinterpret_cast<size_t> (x.get());
1206 if (this->
df_cache.find(
hash) == this->df_cache.end()) {
1228 if (registers.find(
this) == registers.end()) {
1229 auto l = this->
left->compile(stream, registers,
1233 if (!temp.get() || !temp->is_integer()) {
1234 r = this->
right->compile(stream, registers,
1239 stream <<
" const ";
1241 stream <<
" " << registers[
this] <<
" = ";
1242 if (temp.get() && temp->is_integer()) {
1243 stream << registers[l.get()];
1244 const size_t end =
static_cast<size_t> (std::real(this->
right->evaluate().at(0)));
1245 for (
size_t i = 1;
i < end;
i++) {
1246 stream <<
"*" << registers[l.get()];
1250 << registers[l.get()] <<
", "
1251 << registers[r.get()] <<
")";
1256 return this->shared_from_this();
1266 if (
this == x.get()) {
1272 return this->
left->is_match(x_cast->get_left()) &&
1273 this->
right->is_match(x_cast->get_right());
1287 std::cout <<
"\\left(";
1289 this->
left->to_latex();
1291 std::cout <<
"\\right)";
1294 this->
right->to_latex();
1307 if (registers.find(
this) == registers.end()) {
1309 registers[
this] = name;
1310 stream <<
" " << name
1311 <<
" [label = \"pow\", shape = oval, style = filled, fillcolor = blue, fontcolor = white];" << std::endl;
1313 auto l = this->
left->to_vizgraph(stream, registers);
1314 stream <<
" " << name <<
" -- " << registers[l.get()] <<
";" << std::endl;
1315 auto r = this->
right->to_vizgraph(stream, registers);
1316 stream <<
" " << name <<
" -- " << registers[r.get()] <<
";" << std::endl;
1319 return this->shared_from_this();
1328 return this->
left->is_all_variables() &&
1329 (this->
right->is_all_variables() ||
1367 return pow(this->
left->remove_pseudo(),
1368 this->right->remove_pseudo());
1370 return this->shared_from_this();
1383 template<jit::
float_scalar T,
bool SAFE_MATH=false>
1386 auto temp = std::make_shared<pow_node<T, SAFE_MATH>> (l, r)->reduce();
1388 for (
size_t i = temp->get_hash();
1389 i < std::numeric_limits<size_t>::max();
i++) {
1398#if defined(__clang__) || defined(__GNUC__)
1399 __builtin_unreachable();
1401 assert(
false &&
"Should never reach.");
1415 template<jit::
float_scalar T, jit::
float_scalar L,
bool SAFE_MATH=false>
1431 template<jit::
float_scalar T, jit::
float_scalar R,
bool SAFE_MATH=false>
1438 template<jit::
float_scalar T,
bool SAFE_MATH=false>
1447 template<jit::
float_scalar T,
bool SAFE_MATH=false>
1449 return std::dynamic_pointer_cast<pow_node<T, SAFE_MATH>> (x);
1463 template<std::
floating_po
int T,
bool SAFE_MATH=false>
1517 ap2->get_num_columns(),
1522 return this->shared_from_this();
1538 const size_t hash =
reinterpret_cast<size_t> (x.get());
1539 if (this->
df_cache.find(
hash) == this->df_cache.end()) {
1541 * std::numbers::inv_sqrtpi_v<T>
1542 *
exp(this->
arg*this->
arg)*this->arg->df(x);
1561 if (registers.find(
this) == registers.end()) {
1562 auto a = this->
arg->compile(stream, registers,
1566 stream <<
" const ";
1568 stream <<
" " << registers[
this] <<
" = erf("
1569 << registers[a.get()] <<
")";
1573 return this->shared_from_this();
1583 if (
this == x.get()) {
1589 return this->
arg->is_match(x_cast->get_arg());
1599 std::cout <<
"erf\\left(";
1600 this->
arg->to_latex();
1601 std::cout <<
"\\right)";
1611 return erf(this->
arg->remove_pseudo());
1613 return this->shared_from_this();
1625 if (registers.find(
this) == registers.end()) {
1627 registers[
this] = name;
1628 stream <<
" " << name
1629 <<
" [label = \"erf\", shape = oval, style = filled, fillcolor = blue, fontcolor = white];" << std::endl;
1631 auto a = this->
arg->to_vizgraph(stream, registers);
1632 stream <<
" " << name <<
" -- " << registers[a.get()] <<
";" << std::endl;
1635 return this->shared_from_this();
1648 template<std::
floating_po
int T,
bool SAFE_MATH=false>
1650 auto temp = std::make_shared<erf_node<T, SAFE_MATH>> (x)->reduce();
1652 for (
size_t i = temp->get_hash();
1653 i < std::numeric_limits<size_t>::max();
i++) {
1662#if defined(__clang__) || defined(__GNUC__)
1663 __builtin_unreachable();
1665 assert(
false &&
"Should never reach.");
1670 template<std::
floating_po
int T,
bool SAFE_MATH=false>
1682 template<std::
floating_po
int T,
bool SAFE_MATH=false>
1684 return std::dynamic_pointer_cast<erf_node<T, SAFE_MATH>> (x);
1698 template<jit::complex_scalar T,
bool SAFE_MATH=false>
1752 ap2->get_num_columns(),
1757 return this->shared_from_this();
1773 const size_t hash =
reinterpret_cast<size_t> (x.get());
1774 if (this->
df_cache.find(
hash) == this->df_cache.end()) {
1776 if constexpr(std::same_as<T, std::complex<float>>) {
1777 invsqpi = std::numbers::inv_sqrtpi_v<float>;
1779 invsqpi = std::numbers::inv_sqrtpi_v<double>;
1783 *
exp(this->
arg*this->
arg)*this->arg->df(x);
1802 if (registers.find(
this) == registers.end()) {
1803 auto a = this->
arg->compile(stream, registers,
1807 stream <<
" const ";
1809 stream <<
" " << registers[
this] <<
" = special::erfi("
1810 << registers[a.get()] <<
")";
1814 return this->shared_from_this();
1824 if (
this == x.get()) {
1830 return this->
arg->is_match(x_cast->get_arg());
1840 std::cout <<
"erfi\\left(";
1841 this->
arg->to_latex();
1842 std::cout <<
"\\right)";
1852 return erfi(this->
arg->remove_pseudo());
1854 return this->shared_from_this();
1866 if (registers.find(
this) == registers.end()) {
1868 registers[
this] = name;
1869 stream <<
" " << name
1870 <<
" [label = \"erfi\", shape = oval, style = filled, fillcolor = blue, fontcolor = white];" << std::endl;
1872 auto a = this->
arg->to_vizgraph(stream, registers);
1873 stream <<
" " << name <<
" -- " << registers[a.get()] <<
";" << std::endl;
1876 return this->shared_from_this();
1889 template<jit::complex_scalar T,
bool SAFE_MATH=false>
1891 auto temp = std::make_shared<erfi_node<T, SAFE_MATH>> (x)->reduce();
1893 for (
size_t i = temp->get_hash();
1894 i < std::numeric_limits<size_t>::max();
i++) {
1903#if defined(__clang__) || defined(__GNUC__)
1904 __builtin_unreachable();
1906 assert(
false &&
"Should never reach.");
1911 template<jit::complex_scalar T,
bool SAFE_MATH=false>
1923 template<jit::complex_scalar T,
bool SAFE_MATH=false>
1925 return std::dynamic_pointer_cast<erfi_node<T, SAFE_MATH>> (x);
1939 template<std::
floating_po
int T,
bool SAFE_MATH=false>
1989 if (rc.get() && rc->is(0)) {
1991 }
else if (lc.get() && lc->is(0)) {
1993 }
else if (rc.get() && lc.get()) {
1999 if (pl1.get() && (rc.get() || pl1->is_arg_match(this->right))) {
2001 }
else if (pr1.get() && (lc.get() || pr1->is_arg_match(this->left))) {
2007 if (pl2.get() && (rc.get() || pl2->is_arg_match(this->right))) {
2009 pl2->get_num_columns(),
2012 }
else if (pr2.get() && (lc.get() || pr2->is_arg_match(this->left))) {
2014 pr2->get_num_columns(),
2020 if (pr2.get() && pr2->is_row_match(this->left)) {
2024 pr2->get_num_columns(),
2027 }
else if (pr2.get() && pr2->is_col_match(this->left)) {
2031 pr2->get_num_columns(),
2034 }
else if (pl2.get() && pl2->is_row_match(this->right)) {
2038 pl2->get_num_columns(),
2041 }
else if (pl2.get() && pl2->is_col_match(this->right)) {
2045 pl2->get_num_columns(),
2055 if (sql.get() && sqr.get()) {
2056 return sqrt(sql->get_arg() + sqr->get_arg());
2057 }
else if (sql.get()) {
2059 static_cast<T
> (2)));
2060 }
else if (sqr.get()) {
2062 static_cast<T
> (2)));
2066 if (this->
left->is_match(this->right)) {
2067 return std::numbers::sqrt2_v<T>*
sqrt(
pow(this->
left,
2068 static_cast<T
> (2)));
2071 return this->shared_from_this();
2088 const size_t hash =
reinterpret_cast<size_t> (x.get());
2089 if (this->
df_cache.find(
hash) == this->df_cache.end()) {
2092 / this->shared_from_this();
2111 if (registers.find(
this) == registers.end()) {
2112 auto l = this->
left->compile(stream, registers,
2114 auto r = this->
right->compile(stream, registers,
2118 stream <<
" const ";
2120 stream <<
" " << registers[
this] <<
" = ";
2122 stream <<
"length({";
2126 stream << registers[l.get()] <<
", " << registers[r.get()];
2136 return this->shared_from_this();
2146 if (
this == x.get()) {
2153 return (this->
left->is_match(x_cast->get_left()) &&
2154 this->right->is_match(x_cast->get_right())) ||
2155 (this->
right->is_match(x_cast->get_left()) &&
2156 this->left->is_match(x_cast->get_right()));
2166 std::cout <<
"hypot\\left(";
2167 this->
left->to_latex();
2169 this->
right->to_latex();
2170 std::cout <<
"\\right)";
2180 return hypot(this->
left->remove_pseudo(),
2181 this->right->remove_pseudo());
2183 return this->shared_from_this();
2195 if (registers.find(
this) == registers.end()) {
2197 registers[
this] = name;
2198 stream <<
" " << name
2199 <<
" [label = \"hypot\", shape = oval, style = filled, fillcolor = blue, fontcolor = white];" << std::endl;
2201 auto l = this->
left->to_vizgraph(stream, registers);
2202 stream <<
" " << name <<
" -- " << registers[l.get()] <<
";" << std::endl;
2203 auto r = this->
right->to_vizgraph(stream, registers);
2204 stream <<
" " << name <<
" -- " << registers[r.get()] <<
";" << std::endl;
2207 return this->shared_from_this();
2221 template<std::
floating_po
int T,
bool SAFE_MATH=false>
2224 auto temp = std::make_shared<hypot_node<T, SAFE_MATH>> (l, r)->reduce();
2226 for (
size_t i = temp->get_hash();
i < std::numeric_limits<size_t>::max();
i++) {
2235#if defined(__clang__) || defined(__GNUC__)
2236 __builtin_unreachable();
2238 assert(
false &&
"Should never reach.");
2253 template<std::
floating_po
int T, jit::
float_scalar L,
bool SAFE_MATH=false>
2270 template<std::
floating_po
int T, jit::
float_scalar R,
bool SAFE_MATH=false>
2277 template<std::
floating_po
int T,
bool SAFE_MATH=false>
2286 template<std::
floating_po
int T,
bool SAFE_MATH=false>
2288 return std::dynamic_pointer_cast<hypot_node<T, SAFE_MATH>> (x);
2302 template<std::
floating_po
int T,
bool SAFE_MATH=false>
2304 branch_node<T, SAFE_MATH>> {
2355 if (rc.get() && rc->is(0)) {
2357 }
else if (lc.get() && lc->is(0)) {
2359 }
else if (rc.get() && lc.get()) {
2365 if (pl1.get() && (rc.get() || pl1->is_arg_match(this->right))) {
2367 }
else if (pr1.get() && (lc.get() || pr1->is_arg_match(this->left))) {
2373 if (pl2.get() && (rc.get() || pl2->is_arg_match(this->right))) {
2375 pl2->get_num_columns(),
2378 }
else if (pr2.get() && (lc.get() || pr2->is_arg_match(this->left))) {
2380 pr2->get_num_columns(),
2386 if (pr2.get() && pr2->is_row_match(this->left)) {
2390 pr2->get_num_columns(),
2393 }
else if (pr2.get() && pr2->is_col_match(this->left)) {
2397 pr2->get_num_columns(),
2400 }
else if (pl2.get() && pl2->is_row_match(this->right)) {
2404 pl2->get_num_columns(),
2407 }
else if (pl2.get() && pl2->is_col_match(this->right)) {
2411 pl2->get_num_columns(),
2416 return this->shared_from_this();
2433 if (registers.find(
this) == registers.end()) {
2434 auto l = this->
left->compile(stream, registers,
2436 auto r = this->
right->compile(stream, registers,
2440 stream <<
" const ";
2442 stream <<
" " << registers[
this] <<
" = copysign("
2443 << registers[l.get()] <<
", "
2444 << registers[r.get()] <<
")";
2449 return this->shared_from_this();
2459 if (
this == x.get()) {
2465 return (this->
left->is_match(x_cast->get_left()) &&
2466 this->right->is_match(x_cast->get_right()));
2476 std::cout <<
"copysign\\left(";
2477 this->
left->to_latex();
2479 this->
right->to_latex();
2480 std::cout <<
"\\right)";
2491 this->right->remove_pseudo());
2493 return this->shared_from_this();
2505 if (registers.find(
this) == registers.end()) {
2507 registers[
this] = name;
2508 stream <<
" " << name
2509 <<
" [label = \"copysign\", shape = oval, style = filled, fillcolor = blue, fontcolor = white];" << std::endl;
2511 auto l = this->
left->to_vizgraph(stream, registers);
2512 stream <<
" " << name <<
" -- " << registers[l.get()] <<
";" << std::endl;
2513 auto r = this->
right->to_vizgraph(stream, registers);
2514 stream <<
" " << name <<
" -- " << registers[r.get()] <<
";" << std::endl;
2517 return this->shared_from_this();
2531 template<std::
floating_po
int T,
bool SAFE_MATH=false>
2534 auto temp = std::make_shared<copysign_node<T, SAFE_MATH>> (l, r)->reduce();
2536 for (
size_t i = temp->get_hash();
i < std::numeric_limits<size_t>::max();
i++) {
2545#if defined(__clang__) || defined(__GNUC__)
2546 __builtin_unreachable();
2548 assert(
false &&
"Should never reach.");
2563 template<std::
floating_po
int T, jit::
float_scalar L,
bool SAFE_MATH=false>
2580 template<std::
floating_po
int T, jit::
float_scalar R,
bool SAFE_MATH=false>
2587 template<std::
floating_po
int T,
bool SAFE_MATH=false>
2596 template<std::
floating_po
int T,
bool SAFE_MATH=false>
2598 return std::dynamic_pointer_cast<copysign_node<T, SAFE_MATH>> (x);
Class representing a generic buffer.
Definition backend.hpp:29
void erfi()
Take erfi.
Definition backend.hpp:258
void copysign_col(const buffer< T > &x)
copysign col operation.
Definition backend.hpp:841
void log()
Take log.
Definition backend.hpp:223
void sqrt()
Take sqrt.
Definition backend.hpp:209
void hypot_row(const buffer< T > &x)
Hypot row operation.
Definition backend.hpp:733
void pow_col(const buffer< T > &x)
Pow col operation.
Definition backend.hpp:697
void erf()
Take erf.
Definition backend.hpp:251
void pow_row(const buffer< T > &x)
Pow row operation.
Definition backend.hpp:661
void copysign_row(const buffer< T > &x)
copysign row operation.
Definition backend.hpp:805
void hypot_col(const buffer< T > &x)
Hypot col operation.
Definition backend.hpp:769
void exp()
Take exp.
Definition backend.hpp:216
Class representing a branch node.
Definition node.hpp:1336
branch_node(shared_leaf< T, SAFE_MATH > l, shared_leaf< T, SAFE_MATH > r, const std::string s)
Definition node.hpp:1352
shared_leaf< T, SAFE_MATH > right
Definition node.hpp:1341
shared_leaf< T, SAFE_MATH > left
Definition node.hpp:1339
virtual bool is_match(shared_leaf< T, SAFE_MATH > x)
Query if the nodes match.
Definition math.hpp:2458
virtual backend::buffer< T > evaluate()
Evaluate the results of copysign.
Definition math.hpp:2340
virtual shared_leaf< T, SAFE_MATH > remove_pseudo()
Remove pseudo variable nodes.
Definition math.hpp:2488
virtual shared_leaf< T, SAFE_MATH > compile(std::ostringstream &stream, jit::register_map ®isters, const jit::register_map &thread_mem, const jit::register_usage &usage)
Compile the node.
Definition math.hpp:2429
virtual void to_latex() const
Convert the node to latex.
Definition math.hpp:2475
virtual shared_leaf< T, SAFE_MATH > to_vizgraph(std::stringstream &stream, jit::register_map ®isters)
Convert the node to vizgraph.
Definition math.hpp:2503
virtual shared_leaf< T, SAFE_MATH > reduce()
Reduce a copysign node.
Definition math.hpp:2351
copysign_node(shared_leaf< T, SAFE_MATH > l, shared_leaf< T, SAFE_MATH > r)
Construct a hypot node.
Definition math.hpp:2326
virtual backend::buffer< T > evaluate()
Evaluate the results of erf.
Definition math.hpp:1492
virtual shared_leaf< T, SAFE_MATH > to_vizgraph(std::stringstream &stream, jit::register_map ®isters)
Convert the node to vizgraph.
Definition math.hpp:1623
virtual void to_latex() const
Convert the node to latex.
Definition math.hpp:1598
virtual shared_leaf< T, SAFE_MATH > reduce()
Reduce the erf(x).
Definition math.hpp:1503
virtual shared_leaf< T, SAFE_MATH > remove_pseudo()
Remove pseudo variable nodes.
Definition math.hpp:1609
virtual bool is_match(shared_leaf< T, SAFE_MATH > x)
Query if the nodes match.
Definition math.hpp:1582
erf_node(shared_leaf< T, SAFE_MATH > x)
Construct a erf node.
Definition math.hpp:1482
virtual shared_leaf< T, SAFE_MATH > df(shared_leaf< T, SAFE_MATH > x)
Transform node to derivative.
Definition math.hpp:1533
virtual shared_leaf< T, SAFE_MATH > compile(std::ostringstream &stream, jit::register_map ®isters, const jit::register_map &thread_mem, const jit::register_usage &usage)
Compile the node.
Definition math.hpp:1557
virtual shared_leaf< T, SAFE_MATH > compile(std::ostringstream &stream, jit::register_map ®isters, const jit::register_map &thread_mem, const jit::register_usage &usage)
Compile the node.
Definition math.hpp:1798
erfi_node(shared_leaf< T, SAFE_MATH > x)
Construct a erfi node.
Definition math.hpp:1717
virtual shared_leaf< T, SAFE_MATH > reduce()
Reduce the erfi(x).
Definition math.hpp:1738
virtual void to_latex() const
Convert the node to latex.
Definition math.hpp:1839
virtual shared_leaf< T, SAFE_MATH > remove_pseudo()
Remove pseudo variable nodes.
Definition math.hpp:1850
virtual shared_leaf< T, SAFE_MATH > df(shared_leaf< T, SAFE_MATH > x)
Transform node to derivative.
Definition math.hpp:1768
virtual bool is_match(shared_leaf< T, SAFE_MATH > x)
Query if the nodes match.
Definition math.hpp:1823
virtual backend::buffer< T > evaluate()
Evaluate the results of erfi.
Definition math.hpp:1727
virtual shared_leaf< T, SAFE_MATH > to_vizgraph(std::stringstream &stream, jit::register_map ®isters)
Convert the node to vizgraph.
Definition math.hpp:1864
virtual backend::buffer< T > evaluate()
Evaluate the results of exp.
Definition math.hpp:375
virtual shared_leaf< T, SAFE_MATH > df(shared_leaf< T, SAFE_MATH > x)
Transform node to derivative.
Definition math.hpp:422
virtual bool is_match(shared_leaf< T, SAFE_MATH > x)
Query if the nodes match.
Definition math.hpp:491
virtual shared_leaf< T, SAFE_MATH > remove_pseudo()
Remove pseudo variable nodes.
Definition math.hpp:518
exp_node(shared_leaf< T, SAFE_MATH > x)
Construct a exp node.
Definition math.hpp:365
virtual shared_leaf< T, SAFE_MATH > compile(std::ostringstream &stream, jit::register_map ®isters, const jit::register_map &thread_mem, const jit::register_usage &usage)
Compile the node.
Definition math.hpp:444
virtual void to_latex() const
Convert the node to latex.
Definition math.hpp:507
virtual shared_leaf< T, SAFE_MATH > reduce()
Reduce the exp(x).
Definition math.hpp:386
virtual shared_leaf< T, SAFE_MATH > to_vizgraph(std::stringstream &stream, jit::register_map ®isters)
Convert the node to vizgraph.
Definition math.hpp:532
virtual bool is_match(shared_leaf< T, SAFE_MATH > x)
Query if the nodes match.
Definition math.hpp:2145
virtual shared_leaf< T, SAFE_MATH > df(shared_leaf< T, SAFE_MATH > x)
Transform node to derivative.
Definition math.hpp:2083
virtual void to_latex() const
Convert the node to latex.
Definition math.hpp:2165
virtual shared_leaf< T, SAFE_MATH > reduce()
Reduce a hypot node.
Definition math.hpp:1985
virtual shared_leaf< T, SAFE_MATH > compile(std::ostringstream &stream, jit::register_map ®isters, const jit::register_map &thread_mem, const jit::register_usage &usage)
Compile the node.
Definition math.hpp:2107
hypot_node(shared_leaf< T, SAFE_MATH > l, shared_leaf< T, SAFE_MATH > r)
Construct a hypot node.
Definition math.hpp:1962
virtual shared_leaf< T, SAFE_MATH > to_vizgraph(std::stringstream &stream, jit::register_map ®isters)
Convert the node to vizgraph.
Definition math.hpp:2193
virtual shared_leaf< T, SAFE_MATH > remove_pseudo()
Remove pseudo variable nodes.
Definition math.hpp:2178
virtual backend::buffer< T > evaluate()
Evaluate the results of hypot.
Definition math.hpp:1974
Class representing a node leaf.
Definition node.hpp:370
virtual void endline(std::ostringstream &stream, const jit::register_usage &usage, const char end=';') const
Definition node.hpp:652
static caches_t caches
A per thread instance of the cache structure.
Definition node.hpp:682
virtual bool has_pseudo() const
Definition node.hpp:632
const size_t hash
Definition node.hpp:373
std::unordered_map< size_t, std::shared_ptr< leaf_node< T, SAFE_MATH > > > df_cache
Definition node.hpp:377
virtual shared_leaf< T, SAFE_MATH > compile(std::ostringstream &stream, jit::register_map ®isters, const jit::register_map &thread_mem, const jit::register_usage &usage)
Compile the node.
Definition math.hpp:705
virtual shared_leaf< T, SAFE_MATH > to_vizgraph(std::stringstream &stream, jit::register_map ®isters)
Convert the node to vizgraph.
Definition math.hpp:771
virtual shared_leaf< T, SAFE_MATH > df(shared_leaf< T, SAFE_MATH > x)
Transform node to derivative.
Definition math.hpp:683
virtual backend::buffer< T > evaluate()
Evaluate the results of log.
Definition math.hpp:636
virtual bool is_match(shared_leaf< T, SAFE_MATH > x)
Query if the nodes match.
Definition math.hpp:730
virtual shared_leaf< T, SAFE_MATH > reduce()
Reduce the log(x).
Definition math.hpp:647
virtual void to_latex() const
Convert the node to latex.
Definition math.hpp:746
log_node(shared_leaf< T, SAFE_MATH > x)
Construct a log node.
Definition math.hpp:626
virtual shared_leaf< T, SAFE_MATH > remove_pseudo()
Remove pseudo variable nodes.
Definition math.hpp:757
no_derivative(const std::string s)
Definition node.hpp:1723
virtual shared_leaf< T, SAFE_MATH > to_vizgraph(std::stringstream &stream, jit::register_map ®isters)
Convert the node to vizgraph.
Definition math.hpp:1305
virtual bool is_all_variables() const
Test if node acts like a variable.
Definition math.hpp:1327
virtual shared_leaf< T, SAFE_MATH > get_power_base()
Get the base of a power.
Definition math.hpp:1347
virtual shared_leaf< T, SAFE_MATH > compile(std::ostringstream &stream, jit::register_map ®isters, const jit::register_map &thread_mem, const jit::register_usage &usage)
Compile the node.
Definition math.hpp:1224
virtual backend::buffer< T > evaluate()
Evaluate the results of pow.
Definition math.hpp:880
pow_node(shared_leaf< T, SAFE_MATH > l, shared_leaf< T, SAFE_MATH > r)
Construct a power node.
Definition math.hpp:868
virtual bool is_power_like() const
Test if the node acts like a power of variable.
Definition math.hpp:1338
virtual shared_leaf< T, SAFE_MATH > remove_pseudo()
Remove pseudo variable nodes.
Definition math.hpp:1365
virtual bool is_match(shared_leaf< T, SAFE_MATH > x)
Query if the nodes match.
Definition math.hpp:1265
virtual shared_leaf< T, SAFE_MATH > get_power_exponent() const
Get the exponent of a power.
Definition math.hpp:1356
virtual void to_latex() const
Convert the node to latex.
Definition math.hpp:1282
virtual shared_leaf< T, SAFE_MATH > reduce()
Reduce a power node.
Definition math.hpp:891
virtual shared_leaf< T, SAFE_MATH > df(shared_leaf< T, SAFE_MATH > x)
Transform node to derivative.
Definition math.hpp:1200
virtual shared_leaf< T, SAFE_MATH > get_power_exponent() const
Get the exponent of a power.
Definition math.hpp:248
virtual shared_leaf< T, SAFE_MATH > compile(std::ostringstream &stream, jit::register_map ®isters, const jit::register_map &thread_mem, const jit::register_usage &usage)
Compile the node.
Definition math.hpp:178
virtual backend::buffer< T > evaluate()
Evaluate the results of sqrt.
Definition math.hpp:55
virtual shared_leaf< T, SAFE_MATH > reduce()
Reduce the sqrt(x).
Definition math.hpp:66
sqrt_node(shared_leaf< T, SAFE_MATH > x)
Construct a sqrt node.
Definition math.hpp:45
virtual bool is_power_like() const
Test if the node acts like a power of variable.
Definition math.hpp:230
virtual bool is_match(shared_leaf< T, SAFE_MATH > x)
Query if the nodes match.
Definition math.hpp:203
virtual shared_leaf< T, SAFE_MATH > remove_pseudo()
Remove pseudo variable nodes.
Definition math.hpp:257
virtual shared_leaf< T, SAFE_MATH > df(shared_leaf< T, SAFE_MATH > x)
Transform node to derivative.
Definition math.hpp:155
virtual shared_leaf< T, SAFE_MATH > to_vizgraph(std::stringstream &stream, jit::register_map ®isters)
Convert the node to vizgraph.
Definition math.hpp:271
virtual void to_latex() const
Convert the node to latex.
Definition math.hpp:219
virtual shared_leaf< T, SAFE_MATH > get_power_base()
Get the base of a power.
Definition math.hpp:239
shared_leaf< T, SAFE_MATH > arg
Definition node.hpp:1221
straight_node(shared_leaf< T, SAFE_MATH > a, const std::string s)
Definition node.hpp:1230
Complex scalar concept.
Definition register.hpp:25
subroutine assert(test, message)
Assert check.
Definition f_binding_test.f90:38
buffer< T > pow(buffer< T > &base, buffer< T > &exponent)
Take the power.
Definition backend.hpp:1510
buffer< T > copysign(buffer< T > &x, buffer< T > &y)
Copy the sign of x and apply it to y.
Definition backend.hpp:1627
buffer< T > hypot(buffer< T > &a, buffer< T > &b)
hypot operation.
Definition backend.hpp:1177
Name space for graph nodes.
Definition arithmetic.hpp:13
shared_piecewise_2D< T, SAFE_MATH > piecewise_2D_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a piecewise 2D node.
Definition piecewise.hpp:1508
std::shared_ptr< erfi_node< T, SAFE_MATH > > shared_erfi
Convenience type alias for shared erfi nodes.
Definition math.hpp:1912
shared_leaf< T, SAFE_MATH > log(shared_leaf< T, SAFE_MATH > x)
Define log convenience function.
Definition math.hpp:797
shared_pow< T, SAFE_MATH > pow_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a power node.
Definition math.hpp:1448
shared_sqrt< T, SAFE_MATH > sqrt_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a sqrt node.
Definition math.hpp:331
std::shared_ptr< erf_node< T, SAFE_MATH > > shared_erf
Convenience type alias for shared erf nodes.
Definition math.hpp:1671
std::shared_ptr< pow_node< T, SAFE_MATH > > shared_pow
Convenience type alias for shared add nodes.
Definition math.hpp:1439
shared_leaf< T, SAFE_MATH > pow(shared_leaf< T, SAFE_MATH > l, shared_leaf< T, SAFE_MATH > r)
Build power node.
Definition math.hpp:1384
shared_divide< T, SAFE_MATH > divide_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a divide node.
Definition arithmetic.hpp:3802
shared_piecewise_1D< T, SAFE_MATH > piecewise_1D_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a piecewise 1D node.
Definition piecewise.hpp:849
shared_multiply< T, SAFE_MATH > multiply_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a multiply node.
Definition arithmetic.hpp:2843
std::shared_ptr< sqrt_node< T, SAFE_MATH > > shared_sqrt
Convenience type alias for shared sqrt nodes.
Definition math.hpp:319
shared_leaf< T, SAFE_MATH > erf(shared_leaf< T, SAFE_MATH > x)
Define erf convenience function.
Definition math.hpp:1649
constexpr shared_leaf< T, SAFE_MATH > one()
Forward declare for one.
Definition node.hpp:1166
shared_leaf< T, SAFE_MATH > exp(shared_leaf< T, SAFE_MATH > x)
Define exp convenience function.
Definition math.hpp:558
shared_leaf< T, SAFE_MATH > copysign(shared_leaf< T, SAFE_MATH > l, shared_leaf< T, SAFE_MATH > r)
Build copysign node.
Definition math.hpp:2532
std::shared_ptr< leaf_node< T, SAFE_MATH > > shared_leaf
Convenience type alias for shared leaf nodes.
Definition node.hpp:690
shared_exp< T, SAFE_MATH > exp_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a exp node.
Definition math.hpp:592
shared_constant< T, SAFE_MATH > constant_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a constant node.
Definition node.hpp:1201
shared_leaf< T, SAFE_MATH > erfi(shared_leaf< T, SAFE_MATH > x)
Define erfi convenience function.
Definition math.hpp:1890
bool is_variable_combinable(shared_leaf< T, SAFE_MATH > a, shared_leaf< T, SAFE_MATH > b)
Check if the variable is combinable.
Definition arithmetic.hpp:75
shared_variable< T, SAFE_MATH > variable_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a variable node.
Definition node.hpp:2171
shared_erf< T, SAFE_MATH > erf_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a erf node.
Definition math.hpp:1683
constexpr T i
Convenience type for imaginary constant.
Definition node.hpp:1185
shared_leaf< T, SAFE_MATH > constant(const backend::buffer< T > &d)
Construct a constant.
Definition node.hpp:1110
shared_erfi< T, SAFE_MATH > erfi_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a erfi node.
Definition math.hpp:1924
shared_leaf< T, SAFE_MATH > sqrt(shared_leaf< T, SAFE_MATH > x)
Define sqrt convenience function.
Definition math.hpp:297
shared_log< T, SAFE_MATH > log_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a exp node.
Definition math.hpp:830
std::shared_ptr< exp_node< T, SAFE_MATH > > shared_exp
Convenience type alias for shared exp nodes.
Definition math.hpp:580
std::shared_ptr< copysign_node< T, SAFE_MATH > > shared_copysign
Convenience type alias for shared copysign nodes.
Definition math.hpp:2588
std::shared_ptr< hypot_node< T, SAFE_MATH > > shared_hypot
Convenience type alias for shared hypot nodes.
Definition math.hpp:2278
shared_hypot< T, SAFE_MATH > hypot_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a hypot node.
Definition math.hpp:2287
shared_leaf< T, SAFE_MATH > hypot(shared_leaf< T, SAFE_MATH > l, shared_leaf< T, SAFE_MATH > r)
Build hypot node.
Definition math.hpp:2222
std::shared_ptr< log_node< T, SAFE_MATH > > shared_log
Convenience type alias for shared log nodes.
Definition math.hpp:818
shared_copysign< T, SAFE_MATH > copysign_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a copysign node.
Definition math.hpp:2597
constexpr bool use_metal()
Test to use metal.
Definition register.hpp:82
std::string format_to_string(const T value)
Convert a value to a string while avoiding locale.
Definition register.hpp:213
std::unordered_map< void *, size_t > register_usage
Type alias for counting register usage.
Definition register.hpp:261
void add_type(std::basic_ostream< char > &stream)
Write out the node base type to a general stream.
Definition register.hpp:165
std::unordered_map< void *, std::string > register_map
Type alias for mapping node pointers to register names.
Definition register.hpp:259
std::string to_string(const char prefix, const NODE *pointer)
Convert a graph::leaf_node pointer to a string.
Definition register.hpp:247
Base nodes of graph computation framework.
void piecewise_1D()
Tests for 1D piecewise nodes.
Definition piecewise_test.cpp:82
void piecewise_2D()
Tests for 2D piecewise nodes.
Definition piecewise_test.cpp:321