-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmetric.py
843 lines (592 loc) · 22.9 KB
/
metric.py
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
"""
>>> from numpy import array, max, abs, dot
This is a tetrahedral geometry:
>>> w = 0.39685026
>>> A = array([[ w, w, w],
... [-w, -w, w],
... [ w, -w, -w],
... [-w, w, -w]])
Let the volume of the thetrahedron be the "reaction coordiante" or a
"property" of the system:
>>> from rc import Volume
>>> v = Volume()
This choice of the reaction coordinate appears to lead to a "negative"
volume. This is an artifact of the orientation:
>>> v(A)
-0.99999997738152069
Let us "measure" the changes of geometry by the absolute value of the
volumen change:
>>> m = Metric(v)
This is a change in geometry corresponding to breathig mode:
>>> dA = 0.0001 * A
This is the corresponding "measure":
>>> m.norm_up(dA, A)
0.00029999999321445622
Compare that with a central difference:
>>> v(A - dA / 2.) - v(A + dA / 2.)
0.00029999999346475015
And two one-sided differences:
>>> v(A - dA) - v(A)
0.00029996999421522119
>>> v(A + dA) - v(A)
-0.00030002999421374632
Metric is unsigned, of course:
>>> m.norm_up(dA, A) == m.norm_up(-dA, A)
True
"""
from numpy import dot, array, asarray, size, copy
from numpy import sqrt
from numpy import zeros, empty, eye, shape
from numpy.linalg import solve, norm
__all__ = ["cartesian_norm", "setup_metric", "metric"]
def cartesian_norm(dx, x):
"""
Default cartesian norm of |dx|, |x| is ignored. Works with
arguments of any shape, not only with 1D arrays. Emulates the
interface of norm_up/norm_down methods of the classes below.
"""
return norm(dx)
class Default:
"""
Implements metric relevant functions, like for transforming
contra- and covariant vectors into each other
This variant supposes that contra- and covariant vectors are the
same, coordinate transformation if provided is ignored:
>>> met = Default(None)
Contravariant coordinates of a vector:
>>> dX = array([0.0002, 0.001, 0.001])
Get covariant coordinates, position of vectors is ignored:
>>> dx = met.lower(dX, None)
Verify that self.lower changes nothing:
>>> all(dX == dx)
True
Verify self.raises for consistency:
>>> all(dX == met.raises(dx, None))
True
"""
def __init__(self, fun = None):
"""
Normally needs a function with a fprime function in this
special case it is only asked for it because of consistency
"""
pass
def lower(self, dX, X):
return copy(dX)
def raises(self, dx, X):
return copy(dx)
def norm_up(self, dX, X):
dx = self.lower(dX, X)
# flat views of dx and dX:
dx_ = dx.reshape(-1)
dX_ = dX.reshape(-1)
return sqrt(dot(dX_, dx_))
def norm_down(self, dx, X):
dX = self.raises(dx, X)
# flat views of dx and dX:
dx_ = dx.reshape(-1)
dX_ = dX.reshape(-1)
return sqrt(dot(dX_, dx_))
def g (self, X):
"""
Returns a "matrix" representation of the metric at X. It is
only then a true matrix when X is 1D array. In general shape
(g) = shape (X) + shape (X).
"""
# Components here will be flipped to 1:
dX = zeros (shape (X))
# flat view dX:
dX_ = dX.reshape (-1)
g = empty ((size (X), size (X)))
for i in xrange (size (X)):
dX_[i] = 1.0
dx = self.lower (dX, X)
g[:, i] = dx.reshape (-1)
dX_[i] = 0.0
g.shape = shape (X) + shape (X)
return g
def __str__(self):
return "Metric: Working with Metric direct internals (Default)"
class Metric(Default):
"""
Includes metrix relevant functions, like for transforming contra-
and covariant vectors into each other
>>> from numpy import pi, max, abs
Use spherical coordiantes for testing:
>>> from quat import r3
>>> met = Metric(r3)
Arbitrary point in space:
>>> Y = array([8., pi/2. - 0.3, 0.2])
Contravariant coordiantes of a vector:
>>> dY = array([0.0002, 0.001, 0.001])
Covariant coordinates of the vector:
>>> dy = met.lower(dY, Y)
Verify that back transformation gives the original vector:
>>> max(abs(dY - met.raises(dy, Y))) < 1e-15
True
Square norm of the vector:
>>> dy2 = dot(dY, dy)
Cartesian vector and its square:
>>> dx = r3(Y + dY) - r3(Y)
>>> dx2 = dot(dx, dx)
Both should be similar, but not identical:
>>> abs(dy2 - dx2) < 1e-6
True
Use norm function of metric (attention, they give back the sqrt)
>>> abs(met.norm_up(dY, Y) - sqrt(dy2)) < 1e-15
True
>>> abs(met.norm_down(dy, Y) - sqrt(dy2)) < 1e-15
True
FIXME: offering two ways of computing norms here and returning a
square root may be a bad decision. Do not consider "norm_up" and
"norm_down" a part of the Metric interface!
"""
def __init__(self, fun):
"""
needs a function with a fprime function
"""
self.fun = fun
def _fprime_as_matrix(self, X):
"""
Some functions return not linear arrays of different shapes.
Then fprime will be not a 2-dimensional array but more. Thus
transform the result to a matrix as if the function used for
fprime would give back a flattened result.
Not used in this class, maybe in subclasses, but only in this
file! I would let it die.
"""
# FIXME: ensure that "fprime" is always returned as an array instead:
fprime = asarray(self.fun.fprime(X))
# Convert shape of fprime, so that second dimension is length
# of vector x. Rather return a rectangular view of the matrix:
return fprime.reshape(-1, size(X))
def lower(self, dX, X):
"""
Transform contravariant coordiantes dX into covariant
coordiantes dx using the derivatives of the differentiable
transformation at X.
This should also work with rank-deficient matrix of
derivatives B.
"""
dx = empty(shape(dX))
# flat views of dx and dX:
dx_ = dx.reshape(-1)
dX_ = dX.reshape(-1)
# rectangular view of the trafo derivatives:
B = self.fun.fprime(X).reshape(-1, dX_.size)
# destructive update of a locally created var:
dx_[:] = dot(B.T, dot(B, dX_))
# return original view:
return dx
def raises(self, dx, X):
"""
Transform covariant coordiantes dx into contravariant
coordiantes dX using the derivatives of the differentiable
transformation at X.
FIXME: This will fail if the matrix of derivatves B is
rank-deficient and, hence, the corresponding M is singular.
"""
dX = empty(shape(dx))
# flat views of dx and dX:
dx_ = dx.reshape(-1)
dX_ = dX.reshape(-1)
# rectangular view of the trafo derivatives:
B = self.fun.fprime(X).reshape(-1, dx_.size)
M = dot(B.T, B)
# destructive update of a locally created var:
dX_[:] = solve(M, dx_)
# return original view:
return dX
# FIXME: class specific implementation for .g(X) method?
def __str__(self):
return "Metric: Working with Metric Cartesians (Metric)"
class Metric_reduced(Metric):
"""
Includes metrix relevant functions, like for transforming contra-
and covariant vectors into each other.
This one uses Cartesian metric but with ensuring that there are no
global rotation and translation. Therefore it is only designed for
use of some kind of internal coordinates, which do not already
include them.
Potential energy surface of Ar4, energy gradients will be used as
a trial covariant vector:
>>> from ase import Atoms
>>> from qfunc import QFunc
>>> E = QFunc(Atoms("Ar4"))
Tetrahedral geometry, inflated by 5%:
>>> w = 0.39685026 * 1.05
>>> X = array([[ w, w, w],
... [-w, -w, w],
... [ w, -w, -w],
... [-w, w, -w]])
Imagine we use a steepest descent procedure, where step is
proportional to the gradient. In cartesian coordinates this may
look like this:
>>> lam = 0.01
>>> dX = - lam * E.fprime(X)
Coordinate transformation:
>>> from zmat import ZMat
>>> Z = ZMat([(), (0,), (0, 1), (1, 2, 0)])
Internal coordiantes corresponding to the (inflated) geometry:
>>> Y = Z.pinv(X)
Note what effect would a certesian step dX have on internal
coordinates:
>>> dY = Z.pinv(X + dX) - Z.pinv(X)
>>> from numpy import round, max, abs
>>> round(dY, 3)
array([-0.077, -0.077, 0. , -0.077, 0. , 0. ])
Only the bond lengths would shrink by the same amount thus
preserving the symmetry. Now try this with internal coordinates
...
Potential energy surface as a function of internal coordinates:
>>> from func import compose
>>> E1 = compose(E, Z)
What if we made the step proportional to the gradient in internal
coordiantes?
>>> dY = - lam * E1.fprime(Y)
>>> round(dY, 3)
array([-0.039, -0.039, -0.026, -0.039, -0.026, -0.016])
This would also change the angles and break the symmetry. The
reduced metric helps in this case if you remember to never confuse
co- and contravariant coordinates
(FIXME: use analytic derivatives, provided by Z.fprime!):
>>> g = Metric_reduced(Z)
These are the covariant coordiantes of a steepest descent step:
>>> dy = - lam * E1.fprime(Y)
And these are the contravariant ones:
>>> dY = g.raises(dy, Y)
>>> round(dY, 3)
array([-0.077, -0.077, 0. , -0.077, 0. , 0. ])
Consistency check:
>>> max(abs(dy - g.lower(dY, Y))) < 1e-16
True
"""
def lower(self, dY, Y):
"""
Assuming that F is a function to get the derivatives,
transforming vectors of kind vec into cartesians, while f is
the function itself and that all takes place at position pos,
this function transforms contra in covariant vectors with
removing the parts for global coordinates in the covariant
vector space. The transformation between the two is done with
the expression:
vec_i = B.T(I - BT (BT.T Bt)^-1 BT.T - BR (BR.T BR)^-1 BR.T) B vec^i
B(pos) is the transformation just in internals, BT and BR are
the matrices for translation and rotation (special choice with
no interlap)
FIXME: description outdated.
Compute covariant coordinates dy corresponding to
contravariant dY by
T T T
dy = B * ( I - B * g * B - B * g * B ) * B * dY
t t t r r r
with
T -1
g = ( B * B )
t t t
and
T -1
g = ( B * B )
r r r
with B, B, B evaluated at Y.
r t
"""
# positions needed for the global rotation matrix:
X = self.fun(Y)
# get the matrices for the global parameter (without interlap):
BT, gT , BR, gR = B_globals(X)
B = self._fprime_as_matrix(Y)
# this is a cartesian vector corresponding to dY:
dX = dot(B, dY)
# these are components of dX that are translations and rotations,
# respectively:
dXT = dot(BT, dot(gT, dot(BT.T, dX)))
dXR = dot(BR, dot(gR, dot(BR.T, dX)))
return dot(B.T, dX - dXT - dXR)
def raises(self, dy, Y):
"""
Assuming that F is a function to get the derivatives,
transforming vectors of kind vec into cartesians, while f is
the function itself and that all takes place at position pos,
this function transforms co in contravariant vectors with
removing the parts for global coordinates in the covariant
vector space. The transformation between the two is done with
the expression:
vec_i = B.T(I - BT (BT.T Bt)^-1 BT.T - BR (BR.T BR)^-1 BR.T) B vec^i
B(pos) is the transformation just in internals, BT and BR are
the matrices for translation and rotation (special choice with
no interlap)
FIXME: description outdated.
"""
# positions needed for the global rotation matrix:
X = self.fun(Y)
# get the matrices for the global parameter (without
# interlap):
BT, gT , BR, gR = B_globals(X)
B = self._fprime_as_matrix(Y)
#
# See description of self.lower() that defines a linear
# relation
#
# g * dY = dy
#
# between contra- and covariant coordinates, dY and dy, for
# detailed definition of matrix g.
#
# FIXME: isnt modified metric singular? Why do we assume the
# linear equation has a solution? At least the modified
# cartesian metric is singular.
#
# unmodified metric, NY x NY, where NY = dim(Y):
g = dot(B.T, B)
# projections of "internal" modes onto translations and
# rotations, both NY x 3:
T = dot(B.T, BT)
R = dot(B.T, BR)
# modified metric:
g = g - dot(T, dot(gT, T.T)) - dot(R, dot(gR, R.T))
return solve(g, dy)
def __str__(self):
return "Metric: Working with Metric Cartesians with remove of global translation and rotation"
"""
The chosen metric, available are:
* Default, for which contra- and covariant vectors are the same
* Metric, using a Cartesian metric
* Metric_reduced, using a Cartesian metric but removing the effects
of global rotation and translation. This metric makes only sense
for systems in internal coordinates if the coordinates do not
contain already any kind of variables for global positioning. It
should be expected that a change in the global postitions will
have no effect on the energy of the system.
Use print metric to find out which one is set currently. Store and
initalize the choosen metric in metric Here set up the global
variable. Before use make sure that it is initalized with a fitting
variable by function setup_metric. Do not import metric directly, as
it would be the starting version but use for example
import pts.metric as mt
...
co_vec = mt.lower(con_vec)
"""
global metric
metric = Default()
def setup_metric(F = None):
"""
Sets and initalises the metric. F should be a function, which
should when run by itself provide for the internal coordinates y
the corresponding Cartesian coordinates x This function has to be
called once, afterwards all modules should be able to access the
metric.
"""
global metric
metric = Default(F)
def B_globals(carts):
"""
Calculates the matrices BT and BR holding translation and rotation
modes as needed for removing the trivial componets of a
(displacement) vector.
BT is just the derivatives for the translation and is thus
indpendent of geometry. The inverse of the matrix BT^T * BT is
just eye(3) / N.
The matrix BR is build with the geometrical center as rotation
center, thus rotation and translation modes are orthogonal by
construction. The inverse of BR^T * BR is calculated numerically.
Returns a 4-tuple (BT, gT, BR, gR) with 3x3 matrices
T -1
gT = (BT * BT)
and
T -1
gR = (BR * BR)
Translations and rotations modes as returned by this function
are mutually orthogonal:
T
0 = (BT * BR)
Trial geometry (tetrahedron):
>>> from numpy import max, abs
>>> w = 2.0
>>> X = array([[ w, w, w],
... [-w, -w, w],
... [ w, -w, -w],
... [-w, w, -w]])
>>> BT, gT, BR, gR = B_globals(X)
The 2-contravariant tensor
-1
g = G
is an inverse of the 2-covariant Gram matrix
T
G = B * B
for two kinds of trivial vectors, translations and rotatins.
Verify that for rotations ...
>>> GR = dot(BR.T, BR)
>>> max(abs(dot(gR, GR) - eye(3))) < 1e-16
True
>>> max(abs(dot(GR, gR) - eye(3))) < 1e-16
True
and translations ...
>>> GT = dot(BT.T, BT)
>>> max(abs(dot(gT, GT) - eye(3))) < 1e-16
True
>>> max(abs(dot(GT, gT) - eye(3))) < 1e-16
True
Check that translation and rotation modes are mutually orthogonal:
>>> max(abs(dot(BT.T, BR))) < 1e-16
True
Only in this case is it meaningfull to separate the two subspaces.
Below is a somewhat obscure way to verify that translations are
translations and rotations are rotations.
>>> from zmat import RT
Constructor of an RT object expect a differentiable function,
prepare a function that always returns the same tetrahedral
geometry:
>>> def f(y):
... return X
This one also provides derivatives:
>>> from func import NumDiff
>>> f = NumDiff(f)
This one depends on rotational and translational parameters in
addition:
>>> f = RT(f)
Values for rotational and translational parameters:
>>> O = array([0., 0., 0.])
The first argument of f(y, rot, trans) is ignored, so is the
corresponding derivative:
>>> _, BR1, BT1 = f.fprime(O, O, O)
For this particular example derivatives with respect to rotational
and translational parameters coincide:
>>> max(abs(BT.flatten() - BT1.flatten())) < 1e-16
True
>>> max(abs(BR.flatten() - BR1.flatten())) < 1e-16
True
Note that with a different choice of, say, geometrical center of X
offset from origin one cannot expect this agreement anymore.
"""
#
# FIXME: this code necessarily assumes that "carts" is an array of
# cartesian coordinates. Otherwise "translations" and
# "rotation" modes as used implicitly below have no
# meaning. Why cannot we assume that we are always passed
# an (N, 3) array? In fact the code below "assumes" that N
# > 2 otherwise removal of 6 degrees of freedom is not well
# defined. Think of a singular inertia tensor for single
# atom or a diatomic molecule.
#
carts = carts.view().reshape(-1, 3)
# cartesian coordinates with geometrical center as origin:
carts = carts - center(carts)
# number of atoms:
N = len(carts)
# Matrices to hold translation and rotation modes, BT and BR. For
# each of N atoms a 3x3 matrix, or rather for each of 3 modes an
# Nx3 matrix [both will be reshaped to (N*3, 3) later]:
BT = empty((N, 3, 3))
BR = empty((N, 3, 3))
# set the matrices
for i in range(N):
x, y, z = carts[i]
# BT modes for translations, note normalization:
BT[i, :, :] = eye(3)
# BR modes as rotations around the center:
BR[i, :, :] = array([[ 0, z, -y],
[-z, 0, x],
[ y, -x, 0]])
# NOTE: we handle cartesian coordinates as linear arrays in order
# to use numpy.dot(,) later:
BT.shape = (N*3, 3)
BR.shape = (N*3, 3)
# T
# Inverse of BT * BT, that is of pairwise dot products
# of the mode vectors (Gram matrix):
#
gammaT = eye(3) / N
# T
# Inverse of BR * BR, that is of pairwise dot products
# of the mode vectors (Gram matrix):
#
# FIXME: inertia tensor for linear systems is singular!
#
gammaR = inv3(inertia(carts))
return BT, gammaT, BR, gammaR
def center(rs):
"""Compute mass center assuming UNIT masses.
"""
x = zeros((3))
for r in rs:
x += r
return x / len(rs)
def inertia(rs):
"""Compute 3x3 inertia tensor
__
\ 2
I = /_ r * delta - r * r
ij r ij i j
assuming UNIT masses.
>>> w = 2.0
>>> rs = array([[ w, w, w],
... [-w, -w, w],
... [ w, -w, -w],
... [-w, w, -w]])
>>> inertia(rs)
array([[ 32., 0., 0.],
[ 0., 32., 0.],
[ 0., 0., 32.]])
"""
I = zeros((3, 3))
for r in rs:
for i in range(3):
for j in range(3):
I[i, j] -= r[i] * r[j]
I[i, i] += dot(r, r)
return I
def inv3(m):
"""Compute inverse of a 3x3 matrix by Cramers method.
Use scaled rotation matrix for testing:
>>> from numpy import max, abs
>>> from quat import rotmat
>>> m = rotmat(array([0.5, 1.5, 2.])) * 10.0
Inverse:
>>> m1 = inv3(m)
>>> max(abs(dot(m1, m) - eye(3))) < 5e-16
True
>>> max(abs(dot(m, m1) - eye(3))) < 5e-16
True
"""
# adjugate matrix:
w = adj3(m)
# determinant:
D = m[0, 0] * w[0, 0] + m[0, 1] * w[1, 0] + m[0, 2] * w[2, 0]
return w / D
# FIXME: why asarray() is necessary here? The caller
# seems not to accept a matrix() as result:
# return asarray(matrix(m).I)
def adj3(m):
"""Returns adjugate of 3x3 m,
http://en.wikipedia.org/wiki/Adjugate_matrix
Example from Wikipedia:
>>> m = array([[ -3, 2, -5 ],
... [ -1, 0, -2 ],
... [ 3, -4, 1 ]])
>>> adj3(m)
array([[ -8., 18., -4.],
[ -5., 12., -1.],
[ 4., -6., 2.]])
Adjugate of m with permuted rows,
to test the case with m[1, 1] /= 0:
>>> adj3(m[[1, 2, 0]])
array([[ 18., -4., -8.],
[ 12., -1., -5.],
[ -6., 2., 4.]])
"""
w = empty((3, 3))
w[0, 0] = m[1, 1] * m[2, 2] - m[2, 1] * m[1, 2]
w[0, 1] = m[0, 2] * m[2, 1] - m[0, 1] * m[2, 2]
w[0, 2] = m[0, 1] * m[1, 2] - m[1, 1] * m[0, 2]
w[1, 0] = m[2, 0] * m[1, 2] - m[1, 0] * m[2, 2]
w[1, 1] = m[0, 0] * m[2, 2] - m[2, 0] * m[0, 2]
w[1, 2] = m[1, 0] * m[0, 2] - m[0, 0] * m[1, 2]
w[2, 0] = m[1, 0] * m[2, 1] - m[2, 0] * m[1, 1]
w[2, 1] = m[2, 0] * m[0, 1] - m[0, 0] * m[2, 1]
w[2, 2] = m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]
return w
# Testing the examples in __doc__strings, execute
# "python metric.py", eventualy with "-v" option appended:
if __name__ == "__main__":
import doctest
doctest.testmod()