Package sfc :: Package symbolic_utils :: Module symbol_factory
[hide private]
[frames] | no frames]

Source Code for Module sfc.symbolic_utils.symbol_factory

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  """ 
  4  This module contains utility functions for creating symbols and managing symbol contexts. 
  5  """ 
  6   
  7  # Copyright (C) 2007-2008 Martin Sandve Alnes and Simula Resarch Laboratory 
  8  # 
  9  # This file is part of SyFi. 
 10  # 
 11  # SyFi is free software: you can redistribute it and/or modify 
 12  # it under the terms of the GNU General Public License as published by 
 13  # the Free Software Foundation, either version 2 of the License, or 
 14  # (at your option) any later version. 
 15  # 
 16  # SyFi is distributed in the hope that it will be useful, 
 17  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 18  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 19  # GNU General Public License for more details. 
 20  # 
 21  # You should have received a copy of the GNU General Public License 
 22  # along with SyFi. If not, see <http://www.gnu.org/licenses/>. 
 23  # 
 24  # First added:  2007-10-09 
 25  # Last changed: 2008-12-12 
 26   
 27  import math 
 28   
 29  import swiginac 
 30  import SyFi 
 31   
 32   
33 -class SymbolFactory:
34 - def __init__(self):
35 self.symbols = {} 36 for n in ("x", "y", "z", "t", "infinite", "DUMMY"): 37 self.symbols[n] = SyFi.get_symbol(n)
38
39 - def __call__(self, name):
40 if name in self.symbols: 41 return self.symbols[name] 42 s = swiginac.symbol(name) 43 self.symbols[name] = s 44 return s
45 46 # default context for construction of symbols identified by their name 47 _symbol_factory = SymbolFactory() 48 49
50 -class TempSymbolContext:
51 - def __init__(self, format="_s%d", symbol_factory=_symbol_factory):
52 self.format = format 53 self.i = 0 54 self.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 # Functional user interface to the default symbol factory: 63
64 -def symbol_exists(name): # Unused?
65 """Returns a symbol from the default sfc symbol factory.""" 66 return name in _symbol_factory.symbols 67
68 -def symbol(name):
69 """Returns a symbol from the default sfc symbol factory.""" 70 return _symbol_factory(name)
71
72 -def symbolic_vector(n, name):
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
83 -def symbolic_matrix(m, n, name):
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
97 -def symbols(names):
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