[ 1., 1., 1., 1., 1., 1., 1., 1.],

[ 1., 1., 1., 1., 1., 1., 1., 1.],

[ 1., 1., 1., 1., 1., 1., 1., 1.]])


In [42]: C.shape

Out[42]: (4, 8)


In [43]: x

Out[43]: array([1, 2, 3, 4])


In [44]: x.shape

Out[44]: (4,)


In [45]: x.size

Out[45]: 4


In [46]: C.size

Out[46]: 32


In [47]: C = np.ones((4, 4))


In [48]: C

Out[48]:

array([[ 1., 1., 1., 1.],

[ 1., 1., 1., 1.],

[ 1., 1., 1., 1.],

[ 1., 1., 1., 1.]])


In [49]: help(np.linalg.eig)

Help on function eig in module numpy.linalg.linalg:


eig(a)

Compute the eigenvalues and right eigenvectors of a square array.


Parameters

----------

a : (..., M, M) array

Matrices for which the eigenvalues and right eigenvectors will

be computed


Returns

-------

w : (..., M) array

The eigenvalues, each repeated according to its multiplicity.

The eigenvalues are not necessarily ordered. The resulting

array will be of complex type, unless the imaginary part is

zero in which case it will be cast to a real type. When `a`

is real the resulting eigenvalues will be real (0 imaginary

part) or occur in conjugate pairs


v : (..., M, M) array

The normalized (unit "length") eigenvectors, such that the

column ``v[:,i]`` is the eigenvector corresponding to the

eigenvalue ``w[i]``.


Raises

------

LinAlgError

If the eigenvalue computation does not converge.


See Also

--------

eigvals : eigenvalues of a non-symmetric array.


eigh : eigenvalues and eigenvectors of a symmetric or Hermitian

(conjugate symmetric) array.


eigvalsh : eigenvalues of a symmetric or Hermitian (conjugate symmetric)

array.


Notes

-----


.. versionadded:: 1.8.0


Broadcasting rules apply, see the `numpy.linalg` documentation for

details.


This is implemented using the _geev LAPACK routines which compute

the eigenvalues and eigenvectors of general square arrays.


The number `w` is an eigenvalue of `a` if there exists a vector

`v` such that ``dot(a,v) = w * v``. Thus, the arrays `a`, `w`, and

`v` satisfy the equations ``dot(a[:,:], v[:,i]) = w[i] * v[:,i]``

for :math:`i \in \{0,...,M-1\}`.


The array `v` of eigenvectors may not be of maximum rank, that is, some

of the columns may be linearly dependent, although round-off error may

obscure that fact. If the eigenvalues are all different, then theoretically

the eigenvectors are linearly independent. Likewise, the (complex-valued)

matrix of eigenvectors `v` is unitary if the matrix `a` is normal, i.e.,

if ``dot(a, a.H) = dot(a.H, a)``, where `a.H` denotes the conjugate

transpose of `a`.


Finally, it is emphasized that `v` consists of the *right* (as in

right-hand side) eigenvectors of `a`. A vector `y` satisfying

``dot(y.T, a) = z * y.T`` for some number `z` is called a *left*

eigenvector of `a`, and, in general, the left and right eigenvectors

of a matrix are not necessarily the (perhaps conjugate) transposes

of each other.


References

----------

G. Strang, *Linear Algebra and Its Applications*, 2nd Ed., Orlando, FL,

Academic Press, Inc., 1980, Various pp.


Examples

--------

>>> from numpy import linalg as LA


(Almost) trivial example with real e-values and e-vectors.


>>> w, v = LA.eig(np.diag((1, 2, 3)))

>>> w; v

array([ 1., 2., 3.])

array([[ 1., 0., 0.],

[ 0., 1., 0.],

[ 0., 0., 1.]])


Real matrix possessing complex e-values and e-vectors; note that the

e-values are complex conjugates of each other.


>>> w, v = LA.eig(np.array([[1, -1], [1, 1]]))

>>> w; v

array([ 1. + 1.j, 1. - 1.j])

array([[ 0.70710678+0.j , 0.70710678+0.j ],

[ 0.00000000-0.70710678j, 0.00000000+0.70710678j]])


Complex-valued matrix with real e-values (but complex-valued e-vectors);

note that a.conj().T = a, i.e., a is Hermitian.


>>> a = np.array([[1, 1j], [-1j, 1]])

>>> w, v = LA.eig(a)

>>> w; v

array([ 2.00000000e+00+0.j, 5.98651912e-36+0.j]) # i.e., {2, 0}

array([[ 0.00000000+0.70710678j, 0.70710678+0.j ],

[ 0.70710678+0.j , 0.00000000+0.70710678j]])


Be careful about round-off error!


>>> a = np.array([[1 + 1e-9, 0], [0, 1 - 1e-9]])

>>> # Theor. e-values are 1 +/- 1e-9

>>> w, v = LA.eig(a)

>>> w; v

array([ 1., 1.])

array([[ 1., 0.],

[ 0., 1.]])



In [50]: np.linalg.eig(C)

Out[50]:

(array([ 0.00000000e+00, 4.00000000e+00, 0.00000000e+00,

2.80731443e-32]),

array([[-0.8660254 , -0.5 , -0.8660254 , -0.64641535],

[ 0.28867513, -0.5 , 0.28867513, -0.32788993],

[ 0.28867513, -0.5 , 0.28867513, 0.48715264],

[ 0.28867513, -0.5 , 0.28867513, 0.48715264]]))


In [51]: def x6(x):

    ...: return x**6

    ...:


In [52]: x6(0.1)

Out[52]: 1.0000000000000004e-06


In [53]: lambda x: x**6

Out[53]: <function __main__.<lambda>>


In [54]: (lambda x: x**6)

Out[54]: <function __main__.<lambda>>


In [55]: (lambda x: x**6)(0.1)

Out[55]: 1.0000000000000004e-06


In [56]: from scipy.optimize import newton


In [57]: newton(x6, 0.1)

Traceback (most recent call last):


File "<ipython-input-57-25e2a619f6f1>", line 1, in <module>

newton(x6, 0.1)


File "C:\Apps\Anaconda3\lib\site-packages\scipy\optimize\zeros.py", line 184, in newton

raise RuntimeError(msg)


RuntimeError: Failed to converge after 50 iterations, value is 0.00017288881148442095



In [58]: def x2(x):

    ...: return x**2

    ...:


In [59]: newton(x2, 0.1)

Out[59]: 1.7546894263648108e-08


In [60]: newton(lambda x: x**2, 0.1)

Out[60]: 1.7546894263648108e-08


In [61]: from scipy.integrate import quad


In [62]: quad(lambda x: x**2, 0, 2)

Out[62]: (2.666666666666667, 2.960594732333751e-14)


In [63]: quad(lambda x: x**2 + 3 - x, 0, 2)

Out[63]: (6.666666666666666, 7.401486830834377e-14)


In [64]: dir()

Out[64]:

['B',

'C',

'In',

'Out',

'_',

'_22',

'_23',

'_27',

'_28',

'_31',

'_32',

'_33',

'_37',

'_39',

'_41',

'_42',

'_43',

'_44',

'_45',

'_46',

'_48',

'_50',

'_52',

'_53',

'_54',

'_55',

'_59',

'_60',

'_62',

'_63',

'__',

'___',

'__builtin__',

'__builtins__',

'__doc__',

'__loader__',

'__name__',

'__package__',

'__spec__',

'_dh',

'_i',

'_i1',

'_i10',

'_i11',

'_i12',

'_i13',

'_i14',

'_i15',

'_i16',

'_i17',

'_i18',

'_i19',

'_i2',

'_i20',

'_i21',

'_i22',

'_i23',

'_i24',

'_i25',

'_i26',

'_i27',

'_i28',

'_i29',

'_i3',

'_i30',

'_i31',

'_i32',

'_i33',

'_i34',

'_i35',

'_i36',

'_i37',

'_i38',

'_i39',

'_i4',

'_i40',

'_i41',

'_i42',

'_i43',

'_i44',

'_i45',

'_i46',

'_i47',

'_i48',

'_i49',

'_i5',

'_i50',

'_i51',

'_i52',

'_i53',

'_i54',

'_i55',

'_i56',

'_i57',

'_i58',

'_i59',

'_i6',

'_i60',

'_i61',

'_i62',

'_i63',

'_i64',

'_i7',

'_i8',

'_i9',

'_ih',

'_ii',

'_iii',

'_oh',

'_sh',

'exit',

'feps',

'fprime',

'get_ipython',

'i',

'math',

'maxit',

'newton',

'np',

'pylab',

'quad',

'quit',

'rangex',

't',

'x',

'x2',

'x6',

'y',

'z']


In [65]: y = lambda x: x**6


In [66]: y(0.1)

Out[66]: 1.0000000000000004e-06


In [67]: def make_add42():

    ...: def add42(x):

    ...: return x + 42

    ...: return add42

    ...:


In [68]: dir()

Out[68]:

['B',

'C',

'In',

'Out',

'_',

'_22',

'_23',

'_27',

'_28',

'_31',

'_32',

'_33',

'_37',

'_39',

'_41',

'_42',

'_43',

'_44',

'_45',

'_46',

'_48',

'_50',

'_52',

'_53',

'_54',

'_55',

'_59',

'_60',

'_62',

'_63',

'_64',

'_66',

'__',

'___',

'__builtin__',

'__builtins__',

'__doc__',

'__loader__',

'__name__',

'__package__',

'__spec__',

'_dh',

'_i',

'_i1',

'_i10',

'_i11',

'_i12',

'_i13',

'_i14',

'_i15',

'_i16',

'_i17',

'_i18',

'_i19',

'_i2',

'_i20',

'_i21',

'_i22',

'_i23',

'_i24',

'_i25',

'_i26',

'_i27',

'_i28',

'_i29',

'_i3',

'_i30',

'_i31',

'_i32',

'_i33',

'_i34',

'_i35',

'_i36',

'_i37',

'_i38',

'_i39',

'_i4',

'_i40',

'_i41',

'_i42',

'_i43',

'_i44',

'_i45',

'_i46',

'_i47',

'_i48',

'_i49',

'_i5',

'_i50',

'_i51',

'_i52',

'_i53',

'_i54',

'_i55',

'_i56',

'_i57',

'_i58',

'_i59',

'_i6',

'_i60',

'_i61',

'_i62',

'_i63',

'_i64',

'_i65',

'_i66',

'_i67',

'_i68',

'_i7',

'_i8',

'_i9',

'_ih',

'_ii',

'_iii',

'_oh',

'_sh',

'exit',

'feps',

'fprime',

'get_ipython',

'i',

'make_add42',

'math',

'maxit',

'newton',

'np',

'pylab',

'quad',

'quit',

'rangex',

't',

'x',

'x2',

'x6',

'y',

'z']


In [69]: mynewf = make_add42()


In [70]: mynewf

Out[70]: <function __main__.make_add42.<locals>.add42>


In [71]: type(mynewf)

Out[71]: function


In [72]: mynewf(10)

Out[72]: 52


In [73]: