Numworks Epsilon  1.4.1
Graphing Calculator Operating System
square_root.cpp
Go to the documentation of this file.
1 #include <poincare/square_root.h>
2 #include <poincare/complex.h>
3 #include <poincare/power.h>
6 extern "C" {
7 #include <assert.h>
8 }
9 #include <cmath>
10 #include <ion.h>
11 
12 namespace Poincare {
13 
15  return Type::SquareRoot;
16 }
17 
19  SquareRoot * a = new SquareRoot(m_operands, true);
20  return a;
21 }
22 
23 static_assert('\x90' == Ion::Charset::Root, "Unicode error");
24 int SquareRoot::writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignificantDigits) const {
25  return LayoutEngine::writePrefixExpressionTextInBuffer(this, buffer, bufferSize, numberOfSignificantDigits, "\x90");
26 }
27 
28 template<typename T>
29 Complex<T> SquareRoot::computeOnComplex(const Complex<T> c, AngleUnit angleUnit) {
30  if (c.b() == 0 && c.a() >= 0) {
31  return Complex<T>::Float(std::sqrt(c.a()));
32  }
33  return Power::compute(c, Complex<T>::Float(0.5));
34 }
35 
36 Expression * SquareRoot::shallowReduce(Context& context, AngleUnit angleUnit) {
37  Expression * e = Expression::shallowReduce(context, angleUnit);
38  if (e != this) {
39  return e;
40  }
41 #if MATRIX_EXACT_REDUCING
42  if (operand(0)->type() == Type::Matrix) {
43  return SimplificationEngine::map(this, context, angleUnit);
44  }
45 #endif
46  Power * p = new Power(operand(0), new Rational(1, 2), false);
48  replaceWith(p, true);
49  return p->shallowReduce(context, angleUnit);
50 }
51 
52 ExpressionLayout * SquareRoot::privateCreateLayout(PrintFloat::Mode floatDisplayMode, ComplexFormat complexFormat) const {
53  assert(floatDisplayMode != PrintFloat::Mode::Default);
54  assert(complexFormat != ComplexFormat::Default);
55  return new NthRootLayout(operand(0)->createLayout(floatDisplayMode, complexFormat),nullptr);
56 }
57 
58 }
Expression * clone() const override
Definition: square_root.cpp:18
#define assert(e)
Definition: assert.h:9
Expression * replaceWith(Expression *newOperand, bool deleteAfterReplace=true)
Definition: expression.cpp:85
c(generic_all_nodes)
friend class Rational
Definition: expression.h:18
Type type() const override
Definition: square_root.cpp:14
friend class Power
Definition: expression.h:21
const Expression * m_operands[T]
ExpressionLayout * createLayout(PrintFloat::Mode floatDisplayMode=PrintFloat::Mode::Default, ComplexFormat complexFormat=ComplexFormat::Default) const
Definition: expression.cpp:244
static Complex< T > Float(T x)
Definition: complex.cpp:23
static Complex< T > compute(const Complex< T > c, const Complex< T > d)
Definition: power.cpp:84
static int writePrefixExpressionTextInBuffer(const Expression *expression, char *buffer, int bufferSize, int numberOfDigits, const char *operatorName)
const Expression * operand(int i) const
Definition: expression.cpp:78
#define sqrt(x)
Definition: math.h:196
friend class SquareRoot
Definition: expression.h:69