Movatterモバイル変換


[0]ホーム

URL:


SciPy

numpy.trace

numpy.trace(a,offset=0,axis1=0,axis2=1,dtype=None,out=None)[source]

Return the sum along diagonals of the array.

Ifa is 2-D, the sum along its diagonal with the given offsetis returned, i.e., the sum of elementsa[i,i+offset] for all i.

Ifa has more than two dimensions, then the axes specified by axis1 andaxis2 are used to determine the 2-D sub-arrays whose traces are returned.The shape of the resulting array is the same as that ofa withaxis1andaxis2 removed.

Parameters:
a:array_like

Input array, from which the diagonals are taken.

offset:int, optional

Offset of the diagonal from the main diagonal. Can be both positiveand negative. Defaults to 0.

axis1, axis2:int, optional

Axes to be used as the first and second axis of the 2-D sub-arraysfrom which the diagonals should be taken. Defaults are the first twoaxes ofa.

dtype:dtype, optional

Determines the data-type of the returned array and of the accumulatorwhere the elements are summed. If dtype has the value None anda isof integer type of precision less than the default integerprecision, then the default integer precision is used. Otherwise,the precision is the same as that ofa.

out:ndarray, optional

Array into which the output is placed. Its type is preserved andit must be of the right shape to hold the output.

Returns:
sum_along_diagonals:ndarray

Ifa is 2-D, the sum along the diagonal is returned. Ifa haslarger dimensions, then an array of sums along diagonals is returned.

Examples

>>>np.trace(np.eye(3))3.0>>>a=np.arange(8).reshape((2,2,2))>>>np.trace(a)array([6, 8])
>>>a=np.arange(24).reshape((2,2,2,3))>>>np.trace(a).shape(2, 3)

Previous topic

numpy.linalg.slogdet

Next topic

numpy.linalg.solve

Quick search

  • © Copyright 2008-2018, The SciPy community.
  • Last updated on Jul 24, 2018.
  • Created usingSphinx 1.6.6.

[8]ページ先頭

©2009-2025 Movatter.jp