Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 27579d6

Browse files
committed
34128: Improve documentation of symbolic operators
1 parent 12be2d9 commit 27579d6

File tree

1 file changed

+57
-32
lines changed

1 file changed

+57
-32
lines changed

src/sage/symbolic/operators.py

+57-32
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66

77
def add_vararg(first, *rest):
88
r"""
9-
Addition of a variable number of arguments.
9+
Return the sum of all the arguments.
1010
1111
INPUT:
1212
13-
- ``first``, ``rest`` -- arguments to add
13+
- ``first``, ``*rest`` -- arguments to add
1414
15-
OUTPUT: sum of arguments
15+
OUTPUT: sum of the arguments
1616
1717
EXAMPLES::
1818
1919
sage: from sage.symbolic.operators import add_vararg
20-
sage: add_vararg(1,2,3,4,5,6,7)
20+
sage: add_vararg(1, 2, 3, 4, 5, 6, 7)
2121
28
22-
sage: F = (1+x+x^2)
23-
sage: bool(F.operator()(*F.operands()) == F)
22+
sage: x = SR.var('x')
23+
sage: s = 1 + x + x^2 # symbolic sum
24+
sage: bool(s.operator()(*s.operands()) == s)
2425
True
2526
"""
2627
for r in rest:
@@ -30,21 +31,22 @@ def add_vararg(first, *rest):
3031

3132
def mul_vararg(first, *rest):
3233
r"""
33-
Multiplication of a variable number of arguments.
34+
Return the product of all the arguments.
3435
3536
INPUT:
3637
37-
- ``args`` -- arguments to multiply
38+
- ``first``, ``*rest`` -- arguments to multiply
3839
39-
OUTPUT: product of arguments
40+
OUTPUT: product of the arguments
4041
4142
EXAMPLES::
4243
4344
sage: from sage.symbolic.operators import mul_vararg
44-
sage: mul_vararg(9,8,7,6,5,4)
45+
sage: mul_vararg(9, 8, 7, 6, 5, 4)
4546
60480
46-
sage: G = x*cos(x)*sin(x)
47-
sage: bool(G.operator()(*G.operands())==G)
47+
sage: x = SR.var('x')
48+
sage: p = x * cos(x) * sin(x) # symbolic product
49+
sage: bool(p.operator()(*p.operands()) == p)
4850
True
4951
"""
5052
for r in rest:
@@ -69,30 +71,44 @@ def mul_vararg(first, *rest):
6971
operator.ge:'>='}
7072

7173
class FDerivativeOperator():
74+
r"""
75+
Function derivative operators.
76+
77+
A function derivative operator represents a partial derivative
78+
of a function with respect to some variables.
79+
80+
The underlying data are the function, and the parameter set,
81+
a list recording the indices of the variables with respect
82+
to which the partial derivative is taken.
83+
"""
7284
def __init__(self, function, parameter_set):
73-
"""
85+
r"""
86+
Initialize this function derivative operator.
87+
7488
EXAMPLES::
7589
7690
sage: from sage.symbolic.operators import FDerivativeOperator
7791
sage: f = function('foo')
78-
sage: op = FDerivativeOperator(f, [0,1])
92+
sage: op = FDerivativeOperator(f, [0, 1])
7993
sage: loads(dumps(op))
8094
D[0, 1](foo)
8195
"""
8296
self._f = function
8397
self._parameter_set = [int(_) for _ in parameter_set]
8498

8599
def __call__(self, *args):
86-
"""
100+
r"""
101+
Call this function derivative operator on these arguments.
102+
87103
EXAMPLES::
88104
89105
sage: from sage.symbolic.operators import FDerivativeOperator
90-
sage: x,y = var('x,y')
106+
sage: x, y = SR.var('x, y')
91107
sage: f = function('foo')
92-
sage: op = FDerivativeOperator(f, [0,1])
93-
sage: op(x,y)
108+
sage: op = FDerivativeOperator(f, [0, 1])
109+
sage: op(x, y)
94110
diff(foo(x, y), x, y)
95-
sage: op(x,x^2)
111+
sage: op(x, x^2)
96112
D[0, 1](foo)(x, x^2)
97113
98114
TESTS:
@@ -115,56 +131,65 @@ def __call__(self, *args):
115131
# temporary variable e.g. `t0` and then evaluate the
116132
# derivative f'(t0) symbolically at t0=1. See trac
117133
# #12796.
118-
temp_args=SR.temp_var(n=len(args))
119-
vars=[temp_args[i] for i in self._parameter_set]
134+
temp_args = SR.temp_var(n=len(args))
135+
vars = [temp_args[i] for i in self._parameter_set]
120136
return self._f(*temp_args).diff(*vars).function(*temp_args)(*args)
121137
vars = [args[i] for i in self._parameter_set]
122138
return self._f(*args).diff(*vars)
123139

124140
def __repr__(self):
125-
"""
141+
r"""
142+
Return the string representation of this function derivative operator.
143+
126144
EXAMPLES::
127145
128146
sage: from sage.symbolic.operators import FDerivativeOperator
129147
sage: f = function('foo')
130-
sage: op = FDerivativeOperator(f, [0,1]); op
148+
sage: op = FDerivativeOperator(f, [0, 1]); op
131149
D[0, 1](foo)
132150
"""
133-
return "D[%s](%s)"%(", ".join(map(repr, self._parameter_set)), self._f)
151+
return "D[%s](%s)" % (", ".join(map(repr, self._parameter_set)), self._f)
134152

135153
def function(self):
136-
"""
154+
r"""
155+
Return the function associated to this function derivative operator.
156+
137157
EXAMPLES::
138158
139159
sage: from sage.symbolic.operators import FDerivativeOperator
140160
sage: f = function('foo')
141-
sage: op = FDerivativeOperator(f, [0,1])
161+
sage: op = FDerivativeOperator(f, [0, 1])
142162
sage: op.function()
143163
foo
144164
"""
145165
return self._f
146166

147167
def change_function(self, new):
148-
"""
149-
Returns a new FDerivativeOperator with the same parameter set
150-
for a new function.
168+
r"""
169+
Return a new function derivative operator with the same
170+
parameter set but for a new function.
151171
152172
sage: from sage.symbolic.operators import FDerivativeOperator
153173
sage: f = function('foo')
154174
sage: b = function('bar')
155-
sage: op = FDerivativeOperator(f, [0,1])
175+
sage: op = FDerivativeOperator(f, [0, 1])
156176
sage: op.change_function(bar)
157177
D[0, 1](bar)
158178
"""
159179
return FDerivativeOperator(new, self._parameter_set)
160180

161181
def parameter_set(self):
162-
"""
182+
r"""
183+
Return the parameter set of this function derivative operator.
184+
185+
This is the list of indices of variables with respect to which
186+
the derivative is taken.
187+
163188
EXAMPLES::
164189
165190
sage: from sage.symbolic.operators import FDerivativeOperator
166191
sage: f = function('foo')
167-
sage: op = FDerivativeOperator(f, [0,1])
192+
sage: op = FDerivativeOperator(f, [0, 1])
168193
sage: op.parameter_set()
169194
[0, 1]
170195
"""

0 commit comments

Comments
 (0)