File: DisplaySympy.hh

package info (click to toggle)
cadabra2 2.4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 78,732 kB
  • sloc: ansic: 133,450; cpp: 92,064; python: 1,530; javascript: 203; sh: 184; xml: 182; objc: 53; makefile: 51
file content (92 lines) | stat: -rw-r--r-- 3,454 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

#pragma once

#include "DisplayBase.hh"
#include <ostream>
#include <map>
#include <set>

typedef uint32_t kunichar;

namespace cadabra {

	/// \ingroup display
	///
	/// Class to display expressions in a format that Sympy can
	/// parse. Will throw an exception if a Cadabra Ex object cannot be
	/// understood by Sympy. Can also convert expressions back to Cadabra
	/// notation. This class contains the printing logic that you see in
	/// action when you call the `__sympy__` method on an Ex object.

	class DisplaySympy : public DisplayBase {
		public:
			DisplaySympy(const Kernel&, const Ex&);

			/// Rewrite the output of sympy back into a notation used by
			/// Cadabra. This in particular involves converting 'sin' and
			/// friends to `\sin` and so on, as well as converting all the
			/// greek symbols.  Currently only acts node-by-node, does not
			/// do anything complicated with trees.

			void import(Ex&);

			std::string preparse_import(const std::string&);

		protected:
			virtual bool needs_brackets(Ex::iterator it) override;

		private:
			/// Output the expression to a sympy-readable form. For symbols
			/// which cannot be parsed by sympy, this can convert to an
			/// alternative, Such rewrites can then be undone by the
			/// 'import' member above.

			void print_multiplier(std::ostream&, Ex::iterator);
			void print_opening_bracket(std::ostream&, str_node::bracket_t);
			void print_closing_bracket(std::ostream&, str_node::bracket_t);
			void print_parent_rel(std::ostream&, str_node::parent_rel_t, bool first);
			void print_children(std::ostream&, Ex::iterator, int skip=0);


			/// For every object encountered, dispatch will figure out the
			/// most appropriate way to convert it into a LaTeX
			/// expression. This may be done by simply looking at the
			/// object's name (e.g. `\prod` will print as a product) but may
			/// also involve looking up properties and deciding on the best
			/// course of action based on the attached properties.

			virtual void dispatch(std::ostream&, Ex::iterator) override;

			/// Printing members for various standard constructions,
			/// e.g. print as a list, or as a decorated symbol with
			/// super/subscripts etc. The names reflect the structure of the
			/// output, not necessarily the meaning or name of the object
			/// that is being printed.

			void print_productlike(std::ostream&, Ex::iterator, const std::string& inbetween);
			void print_sumlike(std::ostream&, Ex::iterator);
			void print_fraclike(std::ostream&, Ex::iterator);
			void print_commalike(std::ostream&, Ex::iterator);
			void print_arrowlike(std::ostream&, Ex::iterator);
			void print_powlike(std::ostream&, Ex::iterator);
			void print_intlike(std::ostream&, Ex::iterator);
			void print_equalitylike(std::ostream&, Ex::iterator);
			void print_components(std::ostream&, Ex::iterator);
			void print_partial(std::ostream& str, Ex::iterator it);
			void print_matrix(std::ostream& str, Ex::iterator it);
			void print_other(std::ostream& str, Ex::iterator it);

			bool children_have_brackets(Ex::iterator ch) const;

			/// Map from Cadabra symbols to Sympy symbols.
			std::map<std::string, std::string> symmap;
			std::multimap<std::string, std::string> regex_map;

			/// Map from symbols which have had dependencies added
			/// to an expression containing these dependencies.
			std::map<nset_t::iterator, Ex, nset_it_less> depsyms;
		};

	const char *unichar(kunichar c);

	}