1
2
3 """
4 This module contains utility functions for creating symbols and managing symbol contexts.
5 """
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import math
28
29 import swiginac
30 import SyFi
31
32
35 self.symbols = {}
36 for n in ("x", "y", "z", "t", "infinite", "DUMMY"):
37 self.symbols[n] = SyFi.get_symbol(n)
38
45
46
47 _symbol_factory = SymbolFactory()
48
49
51 - def __init__(self, format="_s%d", symbol_factory=_symbol_factory):
55
56 - def __call__(self, shape=None):
57 name = self.format % self.i
58 self.i += 1
59 return self.symbol_factory(name)
60
61
62
63
65 """Returns a symbol from the default sfc symbol factory."""
66 return name in _symbol_factory.symbols
67
69 """Returns a symbol from the default sfc symbol factory."""
70 return _symbol_factory(name)
71
73 """Returns a length n vector of symbols from the default sfc symbol factory.
74 The symbols will be named 'name<i>' with i=0,...,n-1.
75 If n > 10, <i> will be prepadded with zeros."""
76 digits = 1 if n == 1 else 1 + int(math.log10(n-1))
77 rule = "%%s%%0%dd" % digits
78 v = swiginac.matrix(n, 1)
79 for i in xrange(n):
80 v[i] = symbol(rule % (name, i))
81 return v
82
84 """Returns a n x n matrix of symbols from the default sfc symbol factory.
85 If m<10 and n<10, the symbols will be named 'name<i><j>' with i=0,...,m-1, j=0,...,n-1.
86 If m>=10 or n>=10, the symbols will be named 'name_<i>_<j>'."""
87 A = swiginac.matrix(m, n)
88 if m > 9 or n > 9:
89 padding = "_"
90 else:
91 padding = ""
92 for i in xrange(m):
93 for j in xrange(n):
94 A[i, j] = symbol("%s%s%d%s%d" % (name, padding, i, padding, j))
95 return A
96
98 """Returns a list of symbols with names from the default sfc symbol factory.
99 'names' must be an iterable sequence of strings."""
100 return [symbol(na) for na in names]
101