This file is indexed.

/usr/lib/python2.7/dist-packages/Scientific/Geometry/TensorAnalysis.py is in python-scientific 2.9.4-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# This module provides a class representing scalar, vector, and tensor fields.
#
# Written by Konrad Hinsen <hinsen@cnrs-orleans.fr>
# last revision: 2007-6-12
#

"""
Vector and tensor fields with derivatives
"""

from Scientific import N; Numeric = N
from Scientific.Geometry import Vector, Tensor
from Scientific.indexing import index_expression
from Scientific.Functions import Interpolation

#
# General tensor field base class
#
class TensorField(Interpolation.InterpolatingFunction):

    """Tensor field of arbitrary rank

    A tensor field is described by a tensor at each point of
    a three-dimensional rectangular grid. The grid spacing must
    be uniform. Tensor fields are implemented as a subclass
    of InterpolatingFunction from the module
    Scientific.Functions.Interpolation and thus share all methods
    defined in that class.

    Evaluation:

     - 'tensorfield(x, y, z)'   (three coordinates)
     - 'tensorfield(coordinates)'  (sequence containing three coordinates)
    """

    def __init__(self, rank, axes, values, default = None,
                 period = (None, None, None), check = True):
        """
        @param rank: the tensor rank
        @type rank: C{int}

        @param axes: three arrays specifying the axis ticks for the three
                     axes
        @type axes: sequence of C{Numeric.array} of rank 1

        @param values: an array containing the field values. Its first
                       three dimensions correspond to the x, y, z
                       directions and must have lengths compatible with
                       the axis arrays. The remaining dimensions must
                       have length 3.
        @type values: C{Numeric.array} of rank+3 dimensions

        @param default: the value of the field for points outside the grid.
                        A value of 'None' means that an exception will be
                        raised for an attempt to evaluate the field outside
                        the grid. Any other value must a tensor of the
                        correct rank.
        @type default: L{Scientific.Geometry.Tensor} or C{NoneType}

        @param period: the period for each of the variables, or C{None} for
            variables in which the function is not periodic.
        @type period: sequence of length three

        @raise ValueError: if the arguments are not consistent
        """
        if check:
            if len(axes) != 3:
                raise ValueError('Field must have three axes')
            if len(period) != 3:
                raise ValueError('Field requires three period values')
            if len(values.shape) != 3 + rank:
                raise ValueError('Values must have rank ' + `rank`)
            if values.shape[3:] != rank*(3,):
                raise ValueError('Values must have dimension 3')
        self.rank = rank
        self.spacing = []
        for axis in axes:
            d = axis[1:]-axis[:-1]
            self.spacing.append(d[0])
            if check:
                dmin = Numeric.minimum.reduce(d)
                if abs(dmin-Numeric.maximum.reduce(d)) > 0.0001*dmin:
                    raise ValueError('Grid must be equidistant')
        Interpolation.InterpolatingFunction.__init__(self, axes, values,
                                                     default, period)

    def __call__(self, *points):
        if len(points) == 1:
            points = tuple(points[0])
        value = apply(Interpolation.InterpolatingFunction.__call__,
                      (self, ) + points)
        if self.rank == 0:
            return value
        elif self.rank == 1:
            return Vector(value)
        else:
            return Tensor(value)

    def __getitem__(self, index):
        if isinstance(index, int):
            index = (index,)
        rank = self.rank - len(index)
        if rank < 0:
            raise ValueError('Number of indices too large')
        index = index_expression[...] + index + rank*index_expression[::]
        try: default = self.default[index]
        except TypeError: default = None
        if rank == 0:
            return ScalarField(self.axes, self.values[index], default,
                               self.period, False)
        elif rank == 1:
            return VectorField(self.axes, self.values[index], default,
                               self.period, False)
        else:
            return TensorField(self.axes, rank, self.values[index], default,
                               self.period, False)

    def zero(self):
        """
        @returns: a tensor of the same rank as the field values
                  with all elements equal to zero
        @rtype: L{Scientifc.Geometry.Tensor}
        """
        if self.rank == 0:
            return 0.
        else:
            return Tensor(Numeric.zeros(self.rank*(3,), Numeric.Float))

    def derivative(self, variable):
        """
        @param variable: 0 for x, 1 for y, 2 for z
        @type variable: C{int}
        @returns: the derivative with respect to variable
        @rtype: L{TensorField}
        """
        period = self.period[variable]
        if period is None:
            ui = variable*index_expression[::] + \
                 index_expression[2::] + index_expression[...]
            li = variable*index_expression[::] + \
                 index_expression[:-2:] + index_expression[...]
            d_values = 0.5*(self.values[ui]-self.values[li]) \
                       /self.spacing[variable]
            diffaxis = self.axes[variable][1:-1]
            d_axes = self.axes[:variable]+[diffaxis]+self.axes[variable+1:]
        else:
            u = N.take(self.values, range(2, len(self.axes[variable]))+[0, 1],
                       axis=variable)
            l = self.values
            d_values = (u-l)/self.spacing[variable]
            d_axes = self.axes
        d_default = None
        if self.default is not None:
            d_default = Numeric.zeros(self.rank*(3,), Numeric.Float)
        return self._constructor(d_axes, d_values, d_default,
                                 self.period, False)

    def allDerivatives(self):
        """
        @returns: all three derivatives (x, y, z) on equal-sized grids
        @rtype: (L{TensorField}, L{TensorField}, L{TensorField})
        """
        x = self.derivative(0)
        y = self.derivative(1)
        z = self.derivative(2)
        if self.period[0] is None:
            y._reduceAxis(0)
            z._reduceAxis(0)
        if self.period[1] is None:
            x._reduceAxis(1)
            z._reduceAxis(1)
        if self.period[2] is None:
            x._reduceAxis(2)
            y._reduceAxis(2)
        return x, y, z

    def _reduceAxis(self, variable):
        self.axes[variable] = self.axes[variable][1:-1]
        i = variable*index_expression[::] + \
            index_expression[1:-1:] + index_expression[...]
        self.values = self.values[i]

    def _checkCompatibility(self, other):
        if self.period != other.period:
            raise ValueError("Periods don't match")

    def __add__(self, other):
        self._checkCompatibility(other)
        if self.default is None or other.default is None:
            default = None
        else:
            default = self.default + other.default
        return self._constructor(self.axes, self.values+other.values,
                                 default, self.period, False)

    def __sub__(self, other):
        self._checkCompatibility(other)
        if self.default is None or other.default is None:
            default = None
        else:
            default = self.default - other.default
        return self._constructor(self.axes, self.values-other.values,
                                 default, self.period, False)

#
# Scalar field class definition
#
class ScalarField(TensorField):

    """Scalar field (tensor field of rank 0)

    A subclass of TensorField.
    """

    def __init__(self, axes, values, default = None,
                 period = (None, None, None), check = True):
        """
        @param axes: three arrays specifying the axis ticks for the three
                     axes
        @type axes: sequence of C{Numeric.array} of rank 1

        @param values: an array containing the field values. The
                       three dimensions correspond to the x, y, z
                       directions and must have lengths compatible with
                       the axis arrays.
        @type values: C{Numeric.array} of 3 dimensions

        @param default: the value of the field for points outside the grid.
                        A value of 'None' means that an exception will be
                        raised for an attempt to evaluate the field outside
                        the grid. Any other value must a tensor of the
                        correct rank.
        @type default: number or C{NoneType}

        @param period: the period for each of the variables, or C{None} for
            variables in which the function is not periodic.
        @type period: sequence of length three

        @raise ValueError: if the arguments are not consistent
        """
        TensorField.__init__(self, 0, axes, values, default, period, check)

    def gradient(self):
        """
        @returns: the gradient
        @rtype: L{VectorField}
        """
        x, y, z = self.allDerivatives()
        grad = Numeric.transpose(Numeric.array([x.values, y.values, z.values]),
                                 [1,2,3,0])
        if self.default is None:
            default = None
        else:
            default = Numeric.zeros((3,), Numeric.Float)
        return VectorField(x.axes, grad, default, self.period, False)

    def laplacian(self):
        """
        @returns: the laplacian (gradient of divergence)
        @rtype L{ScalarField}
        """
        return self.gradient().divergence()

ScalarField._constructor = ScalarField

#
# Vector field class definition
#
class VectorField(TensorField):

    """Vector field (tensor field of rank 1)

    A subclass of TensorField.
    """

    def __init__(self, axes, values, default = None,
                 period = (None, None, None), check = True):
        """
        @param axes: three arrays specifying the axis ticks for the three
                     axes
        @type axes: sequence of C{Numeric.array} of rank 1

        @param values: an array containing the field values. Its first
                       three dimensions correspond to the x, y, z
                       directions and must have lengths compatible with
                       the axis arrays. The fourth dimension must
                       have length 3.
        @type values: C{Numeric.array} of four dimensions

        @param default: the value of the field for points outside the grid.
                        A value of 'None' means that an exception will be
                        raised for an attempt to evaluate the field outside
                        the grid. Any other value must a vector
        @type default: L{Scientific.Geometry.Vector} or C{NoneType}

        @param period: the period for each of the variables, or C{None} for
            variables in which the function is not periodic.
        @type period: sequence of length three

        @raise ValueError: if the arguments are not consistent
        """
        TensorField.__init__(self, 1, axes, values, default, period, check)

    def zero(self):
        return Vector(0., 0., 0.)

    def _divergence(self, x, y, z):
        return x[0] + y[1] + z[2]

    def _curl(self, x, y, z):
        curl_x = y.values[..., 2] - z.values[..., 1]
        curl_y = z.values[..., 0] - x.values[..., 2]
        curl_z = x.values[..., 1] - y.values[..., 0]
        curl = Numeric.transpose(Numeric.array([curl_x, curl_y, curl_z]),
                                 [1,2,3,0])
        if self.default is None:
            default = None
        else:
            default = Numeric.zeros((3,), Numeric.Float)
        return VectorField(x.axes, curl, default, self.period, False)

    def _strain(self, x, y, z):
        strain = Numeric.transpose(Numeric.array([x.values, y.values,
                                                  z.values]), [1,2,3,0,4])
        strain = 0.5*(strain+Numeric.transpose(strain, [0,1,2,4,3]))
        trace = (strain[..., 0,0] + strain[..., 1,1] + strain[..., 2,2])/3.
        strain = strain - trace[..., Numeric.NewAxis, Numeric.NewAxis] * \
                 Numeric.identity(3)[Numeric.NewAxis, Numeric.NewAxis,
                                     Numeric.NewAxis, :, :]
        if self.default is None:
            default = None
        else:
            default = Numeric.zeros((3, 3), Numeric.Float)
        return TensorField(2, x.axes, strain, default, self.period, False)
        
    def divergence(self):
        """
        @returns: the divergence
        @rtype L{ScalarField}
        """
        x, y, z = self.allDerivatives()
        return self._divergence(x, y, z)

    def curl(self):
        """
        @returns: the curl
        @rtype L{VectorField}
        """
        x, y, z = self.allDerivatives()
        return self._curl(x, y, z)

    def strain(self): 
        """
        @returns: the strain
        @rtype L{TensorField} of rank 2
        """
        x, y, z = self.allDerivatives()
        return self._strain(x, y, z)

    def divergenceCurlAndStrain(self):
        """
        @returns: all derivative fields: divergence, curl, and strain
        @rtype (L{ScalarField}, L{VectorField}, L{TensorField})
        """
        x, y, z = self.allDerivatives()
        return self._divergence(x, y, z), self._curl(x, y, z), \
               self._strain(x, y, z)

    def laplacian(self):
        """
        @returns: the laplacian
        @rtype L{VectorField}
        """
        x, y, z = self.allDerivatives()
        return self._divergence(x, y, z).gradient()-self._curl(x, y, z).curl()

    def length(self):
        """
        @returns: a scalar field corresponding to the length (norm) of
        the vector field.
        @rtype: L{ScalarField}
        """
        l = Numeric.sqrt(Numeric.add.reduce(self.values**2, -1))
        if self.default is None:
            default = None
        else:
            try:
                default = Numeric.sqrt(Numeric.add.reduce(self.default))
            except ValueError:
                default = None
        return ScalarField(self.axes, l, default, self.period, False)

VectorField._constructor = VectorField

#
# Test code
#
if __name__ == '__main__':

    from Numeric import *
    axis = arange(0., 1., 0.1)
    values = zeros((10,10,10,3), Float)

    zero = VectorField(3*(axis,), values)
    div = zero.divergence()
    curl = zero.curl()
    strain = zero.strain()

    zero = VectorField(3*(axis,), values, period=(1.1, 1.1, 1.1))
    div = zero.divergence()
    curl = zero.curl()
    strain = zero.strain()