Loading...
Searching...
No Matches
is_zero.cpp
1#include <symengine/basic.h>
2#include <symengine/assumptions.h>
3#include <symengine/visitor.h>
4#include <symengine/test_visitors.h>
5
6namespace SymEngine
7{
8
9class MatrixZeroVisitor : public BaseVisitor<MatrixZeroVisitor>
10{
11private:
12 tribool is_zero_;
13 const Assumptions *assumptions_;
14
15public:
16 MatrixZeroVisitor(const Assumptions *assumptions)
17 : assumptions_(assumptions)
18 {
19 }
20
21 void bvisit(const Basic &x){};
22 void bvisit(const MatrixExpr &x)
23 {
24 is_zero_ = tribool::indeterminate;
25 }
26
27 void bvisit(const IdentityMatrix &x)
28 {
29 is_zero_ = tribool::trifalse;
30 }
31
32 void bvisit(const ZeroMatrix &x)
33 {
34 is_zero_ = tribool::tritrue;
35 }
36
37 void bvisit(const DiagonalMatrix &x)
38 {
39 tribool current = tribool::tritrue;
40 for (auto &e : x.get_container()) {
41 tribool next = is_zero(*e, assumptions_);
42 if (is_false(next)) {
43 is_zero_ = next;
44 return;
45 }
46 current = andwk_tribool(current, next);
47 }
48 is_zero_ = current;
49 }
50
51 void bvisit(const ImmutableDenseMatrix &x)
52 {
53 ZeroVisitor visitor(assumptions_);
54 is_zero_ = tribool::tritrue;
55 for (auto &e : x.get_values()) {
56 is_zero_ = and_tribool(is_zero_, visitor.apply(*e));
57 if (is_false(is_zero_)) {
58 return;
59 }
60 }
61 }
62
63 void bvisit(const MatrixAdd &x)
64 {
65 is_zero_ = tribool::indeterminate;
66 }
67
68 tribool apply(const MatrixExpr &s)
69 {
70 s.accept(*this);
71 return is_zero_;
72 }
73};
74
75tribool is_zero(const MatrixExpr &m, const Assumptions *assumptions)
76{
77 MatrixZeroVisitor visitor(assumptions);
78 return visitor.apply(m);
79}
80
81} // namespace SymEngine
The base class for SymEngine.
The lowest unit of symbolic representation.
Definition: basic.h:97
Main namespace for SymEngine package.
Definition: add.cpp:19