# HG changeset patch
# User Golam Mortuza Hossain <gmhossain@gmail.com>
# Date 1252185702 10800
# Node ID db8fbca6b7b549d6bbb00b832cbf13d04f7bab76
# Parent bed868ece57991000d41cb5605f94c70974c997c
Allow users to specify domain and latex_name for symbolic variables
diff --git a/sage/calculus/var.pyx b/sage/calculus/var.pyx
|
a
|
b
|
|
| 10 | 10 | - ``s`` - a string, either a single variable name, or a space or |
| 11 | 11 | comma separated list of variable names. |
| 12 | 12 | |
| | 13 | - ``kwds`` - keyword arguments can be given to specify domain and |
| | 14 | custom latex_name for variables. See EXAMPLES for usage. |
| | 15 | |
| 13 | 16 | .. note:: |
| 14 | 17 | |
| 15 | 18 | The new variable is both returned and automatically injected |
| 16 | | into the global namespace. If you use var in library code, it |
| 17 | | is better to use sage.symbolic.ring.var, since it won't |
| 18 | | touch the global namespace. |
| | 19 | into the global namespace. If you need symbolic variable in |
| | 20 | library code, it is better to use either SR.var() or SR.new_var(). |
| 19 | 21 | |
| 20 | 22 | EXAMPLES: |
| 21 | 23 | |
| … |
… |
|
| 29 | 31 | sage: f = xx^n + yy^n + zz^n; f |
| 30 | 32 | xx^n + yy^n + zz^n |
| 31 | 33 | |
| | 34 | By default, var returns complex variable. To define real or positive |
| | 35 | variable we can specify its domain as:: |
| | 36 | |
| | 37 | sage: x = var('x', domain=RR); x; x.conjugate() |
| | 38 | x |
| | 39 | x |
| | 40 | sage: y = var('y', domain='real'); y.conjugate() |
| | 41 | y |
| | 42 | sage: y = var('y', domain='positive'); y.abs() |
| | 43 | y |
| | 44 | |
| | 45 | Custom latex expression can be assigned to variable:: |
| | 46 | |
| | 47 | sage: x = var('sui', latex_name="s_{u,i}"); x._latex_() |
| | 48 | 's_{u,i}' |
| | 49 | |
| | 50 | In notebook, we can also colorize latex expression:: |
| | 51 | |
| | 52 | sage: x = var('sui', latex_name="\\color{red}{s_{u,i}}"); x._latex_() |
| | 53 | '\\color{red}{s_{u,i}}' |
| | 54 | |
| 32 | 55 | We can substitute a new variable name for n:: |
| 33 | 56 | |
| 34 | 57 | sage: f(n = var('sigma')) |
| … |
… |
|
| 81 | 104 | deprecation("The new (Pynac) symbolics are now the only symbolics; please do not use keyword 'ns' any longer.") |
| 82 | 105 | else: |
| 83 | 106 | raise NotImplementedError, "The new (Pynac) symbolics are now the only symbolics; please do not use keyword `ns` any longer." |
| 84 | | v = new_var(s) |
| | 107 | v = new_var(s, **kwds) |
| 85 | 108 | if isinstance(v, tuple): |
| 86 | 109 | for x in v: |
| 87 | 110 | G[repr(x)] = x |
diff --git a/sage/libs/ginac/decl.pxi b/sage/libs/ginac/decl.pxi
|
a
|
b
|
|
| 25 | 25 | |
| 26 | 26 | |
| 27 | 27 | ctypedef struct GSymbol "symbol": |
| 28 | | pass |
| 29 | | |
| | 28 | unsigned get_domain() |
| | 29 | void set_domain(unsigned d) |
| | 30 | void set_texname(char* t) |
| | 31 | |
| 30 | 32 | GSymbol* GSymbol_construct_str "Construct_p<symbol, char*>" \ |
| 31 | 33 | (void *mem, char* m) |
| 32 | 34 | |
| … |
… |
|
| 208 | 210 | GEx g_pow "pow" (GEx left, GEx exp) except + |
| 209 | 211 | |
| 210 | 212 | GSymbol get_symbol(char* s) except + |
| | 213 | GSymbol ginac_symbol "GiNaC::symbol" (char* s, char* t, unsigned d) except + |
| | 214 | GSymbol ginac_new_symbol "GiNaC::symbol" () except + |
| 211 | 215 | GEx g_collect_common_factors "collect_common_factors" (GEx e) except + |
| 212 | 216 | |
| 213 | 217 | # standard library string |
diff --git a/sage/symbolic/ring.pyx b/sage/symbolic/ring.pyx
|
a
|
b
|
|
| 31 | 31 | from sage.structure.parent_base import ParentWithBase |
| 32 | 32 | from sage.rings.ring cimport CommutativeRing |
| 33 | 33 | from sage.categories.morphism cimport Morphism |
| | 34 | |
| | 35 | pynac_symbol_registry = {} |
| 34 | 36 | |
| 35 | 37 | cdef class SymbolicRing(CommutativeRing): |
| 36 | 38 | """ |
| … |
… |
|
| 412 | 414 | from sage.symbolic.constants import pi |
| 413 | 415 | return self(pi) |
| 414 | 416 | |
| 415 | | cpdef symbol(self, name): |
| | 417 | cpdef symbol(self, name=None, latex_name=None, domain=None): |
| 416 | 418 | """ |
| 417 | 419 | EXAMPLES:: |
| 418 | 420 | |
| 419 | | sage: SR.symbol("asdfasdfasdf") |
| 420 | | asdfasdfasdf |
| | 421 | sage: tmp0_var = SR.symbol("tmp0_var") |
| | 422 | sage: tmp0_var.conjugate() |
| | 423 | conjugate(tmp0_var) |
| | 424 | |
| | 425 | sage: tmp0_var = SR.symbol("tmp0_var", domain='real') |
| | 426 | sage: tmp0_var.conjugate() |
| | 427 | tmp0_var |
| | 428 | |
| | 429 | sage: tmp0_var.abs() |
| | 430 | abs(tmp0_var) |
| | 431 | |
| | 432 | sage: tmp0_var = SR.symbol("tmp0_var", domain='positive') |
| | 433 | sage: tmp0_var.abs() |
| | 434 | tmp0_var |
| | 435 | |
| | 436 | sage: tmp0_var = SR.symbol(); tmp0_var #random |
| | 437 | symbol343 |
| 421 | 438 | """ |
| 422 | | cdef GSymbol symb = get_symbol(name) |
| | 439 | # Decide the domain |
| | 440 | from sage.rings.all import RR |
| | 441 | if domain is 'real' or domain is RR: |
| | 442 | domain = domain_real |
| | 443 | elif domain is 'positive': |
| | 444 | domain = domain_positive |
| | 445 | else: |
| | 446 | domain = domain_complex |
| | 447 | cdef GSymbol symb |
| 423 | 448 | cdef Expression e |
| 424 | | e = <Expression>PY_NEW(Expression) |
| 425 | | GEx_construct_symbol(&e._gobj, symb) |
| 426 | | e._parent = SR |
| | 449 | # Check if there is already a symbol of same name |
| | 450 | try: |
| | 451 | e = pynac_symbol_registry[name] |
| | 452 | symb = ex_to_symbol((<Expression>e)._gobj) |
| | 453 | if symb.get_domain() is domain and latex_name is None: |
| | 454 | return e |
| | 455 | else: |
| | 456 | if not symb.get_domain() is domain: |
| | 457 | symb.set_domain(domain) |
| | 458 | if latex_name is not None: |
| | 459 | symb.set_texname(latex_name) |
| | 460 | GEx_construct_symbol(&e._gobj, symb) |
| | 461 | except KeyError: |
| | 462 | # Check if we need a distinct new symbol |
| | 463 | if name is None: |
| | 464 | symb = ginac_new_symbol() |
| | 465 | if not domain is domain_complex: |
| | 466 | symb.set_domain(domain) |
| | 467 | else: |
| | 468 | # Get a ginac symbol with given name |
| | 469 | name = str(name) |
| | 470 | if latex_name is None: |
| | 471 | from sage.misc.latex import latex_variable_name |
| | 472 | latex_name = latex_variable_name(name) |
| | 473 | symb = ginac_symbol(name, latex_name, domain) |
| | 474 | # Construct expression |
| | 475 | e = <Expression>PY_NEW(Expression) |
| | 476 | GEx_construct_symbol(&e._gobj, symb) |
| | 477 | e._parent = SR |
| | 478 | pynac_symbol_registry[repr(e)] = e |
| 427 | 479 | return e |
| 428 | 480 | |
| 429 | | def var(self, name): |
| | 481 | def var(self, name, **kwds): |
| 430 | 482 | """ |
| 431 | 483 | Return the symbolic variable defined by x as an element of the |
| 432 | 484 | symbolic ring. |
| … |
… |
|
| 445 | 497 | if not isinstance(name, str): |
| 446 | 498 | name = repr(name) |
| 447 | 499 | if ',' in name: |
| 448 | | return tuple([self.symbol(s.strip()) for s in name.split(',')]) |
| | 500 | return tuple([self.symbol(s.strip(), **kwds) for s in name.split(',')]) |
| 449 | 501 | elif ' ' in name: |
| 450 | | return tuple([self.symbol(s.strip()) for s in name.split(' ')]) |
| | 502 | return tuple([self.symbol(s.strip(), **kwds) for s in name.split(' ')]) |
| 451 | 503 | else: |
| 452 | | return self.symbol(name) |
| | 504 | return self.symbol(name, **kwds) |
| | 505 | |
| | 506 | def new_var(self, domain=None): |
| | 507 | """ |
| | 508 | Returns a unique symbolic variable as an element of the symbolic ring. |
| | 509 | This is useful for situation where a temporary but distinct variable |
| | 510 | (apart from those already in use) is needed. |
| | 511 | |
| | 512 | EXAMPLES:: |
| | 513 | |
| | 514 | sage: tmpvar = SR.new_var(); tmpvar #random |
| | 515 | symbol343 |
| | 516 | sage: tmpvar = SR.new_var(domain='real'); tmpvar #random |
| | 517 | symbol347 |
| | 518 | """ |
| | 519 | return self.symbol(name=None, domain=domain) |
| 453 | 520 | |
| 454 | 521 | def _repr_element_(self, Expression x): |
| 455 | 522 | """ |
| … |
… |
|
| 667 | 734 | """ |
| 668 | 735 | return R is SR |
| 669 | 736 | |
| 670 | | def var(name): |
| | 737 | def var(name, **kwds): |
| 671 | 738 | """ |
| 672 | 739 | EXAMPLES:: |
| 673 | 740 | |
| … |
… |
|
| 681 | 748 | sage: var("z") |
| 682 | 749 | z |
| 683 | 750 | """ |
| 684 | | return SR.var(name) |
| | 751 | return SR.var(name, **kwds) |
| 685 | 752 | |
| 686 | 753 | def is_SymbolicVariable(x): |
| 687 | 754 | """ |