您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 线性代数-python
LinearAlgebra(scipy.linalg)WhenSciPyisbuiltusingtheoptimizedATLASLAPACKandBLASlibraries,ithasveryfastlinearalgebracapabilities.Ifyoudigdeepenough,alloftherawlapackandblaslibrariesareavailableforyouruseforevenmorespeed.Inthissection,someeasier-to-useinterfacestotheseroutinesaredescribed.Alloftheselinearalgebraroutinesexpectanobjectthatcanbeconvertedintoa2-dimensionalarray.Theoutputoftheseroutinesisalsoatwo-dimensionalarray.scipy.linalgvsnumpy.linalgscipy.linalgcontainsallthefunctionsinnumpy.linalg.plussomeothermoreadvancedonesnotcontainedinnumpy.linalgAnotheradvantageofusingscipy.linalgovernumpy.linalgisthatitisalwayscompiledwithBLAS/LAPACKsupport,whilefornumpythisisoptional.Therefore,thescipyversionmightbefasterdependingonhownumpywasinstalled.Therefore,unlessyoudon’twanttoaddscipyasadependencytoyournumpyprogram,usescipy.linalginsteadofnumpy.linalgnumpy.matrixvs2Dnumpy.ndarrayTheclassesthatrepresentmatrices,andbasicoperationssuchasmatrixmultiplicationsandtransposeareapartofnumpy.Forconvenience,wesummarizethedifferencesbetweennumpy.matrixandnumpy.ndarrayhere.numpy.matrixismatrixclassthathasamoreconvenientinterfacethannumpy.ndarrayformatrixoperations.ThisclasssupportsforexampleMATLAB-likecreationsyntaxviathe,hasmatrixmultiplicationasdefaultforthe*operator,andcontainsIandTmembersthatserveasshortcutsforinverseandtranspose:importnumpyasnpA=np.mat('[12;34]')Amatrix([[1,2],[3,4]])A.Imatrix([[-2.,1.],[1.5,-0.5]])b=np.mat('[56]')bmatrix([[5,6]])b.Tmatrix([[5],[6]])A*b.Tmatrix([[17],[39]])Despiteitsconvenience,theuseofthenumpy.matrixclassisdiscouraged,sinceitaddsnothingthatcannotbeaccomplishedwith2Dnumpy.ndarrayobjects,andmayleadtoaconfusionofwhichclassisbeingused.Forexample,theabovecodecanberewrittenas:importnumpyasnpfromscipyimportlinalgA=np.array([[1,2],[3,4]])Aarray([[1,2],[3,4]])linalg.inv(A)array([[-2.,1.],[1.5,-0.5]])b=np.array([[5,6]])#2Darraybarray([[5,6]])b.Tarray([[5],[6]])A*b#notmatrixmultiplication!array([[5,12],[15,24]])A.dot(b.T)#matrixmultiplicationarray([[17],[39]])b=np.array([5,6])#1Darraybarray([5,6])b.T#notmatrixtranspose!array([5,6])A.dot(b)#doesnotmatterformultiplicationarray([17,39])scipy.linalgoperationscanbeappliedequallytonumpy.matrixorto2Dnumpy.ndarrayobjects.BasicroutinesFindingInverseTheinverseofamatrixisthematrixsuchthatwhereistheidentitymatrixconsistingofonesdownthemaindiagonal.Usuallyisdenoted.InSciPy,thematrixinverseoftheNumpyarray,A,isobtainedusinglinalg.inv(A),orusingA.IifAisaMatrix.Forexample,letthenThefollowingexampledemonstratesthiscomputationinSciPyimportnumpyasnpfromscipyimportlinalgA=np.array([[1,2],[3,4]])array([[1,2],[3,4]])linalg.inv(A)array([[-2.,1.],[1.5,-0.5]])A.dot(linalg.inv(A))#doublecheckarray([[1.00000000e+00,0.00000000e+00],[4.44089210e-16,1.00000000e+00]])SolvinglinearsystemSolvinglinearsystemsofequationsisstraightforwardusingthescipycommandlinalg.solve.Thiscommandexpectsaninputmatrixandaright-hand-sidevector.Thesolutionvectoristhencomputed.Anoptionforenteringasymmetrixmatrixisofferedwhichcanspeeduptheprocessingwhenapplicable.Asanexample,supposeitisdesiredtosolvethefollowingsimultaneousequations:Wecouldfindthesolutionvectorusingamatrixinverse:However,itisbettertousethelinalg.solvecommandwhichcanbefasterandmorenumericallystable.Inthiscaseithowevergivesthesameanswerasshowninthefollowingexample:importnumpyasnpfromscipyimportlinalgA=np.array([[1,2],[3,4]])Aarray([[1,2],[3,4]])b=np.array([[5],[6]])barray([[5],[6]])linalg.inv(A).dot(b)#slowarray([[-4.],[4.5]]A.dot(linalg.inv(A).dot(b))-b#checkarray([[8.88178420e-16],[2.66453526e-15]])np.linalg.solve(A,b)#fastarray([[-4.],[4.5]])A.dot(np.linalg.solve(A,b))-b#checkarray([[0.],[0.]])FindingDeterminantThedeterminantofasquarematrixisoftendenotedandisaquantityoftenusedinlinearalgebra.Supposearetheelementsofthematrixandletbethedeterminantofthematrixleftbyremovingtherowandcolumnfrom.ThenforanyrowThisisarecursivewaytodefinethedeterminantwherethebasecaseisdefinedbyacceptingthatthedeterminantofamatrixistheonlymatrixelement.InSciPythedeterminantcanbecalculatedwithlinalg.det.Forexample,thedeterminantofisInSciPythisiscomputedasshowninthisexample:importnumpyasnpfromscipyimportlinalgA=np.array([[1,2],[3,4]])Aarray([[1,2],[3,4]])linalg.det(A)-2.0ComputingnormsMatrixandvectornormscanalsobecomputedwithSciPy.Awiderangeofnormdefinitionsareavailableusingdifferentparameterstotheorderargumentoflinalg.norm.Thisfunctiontakesarank-1(vectors)orarank-2(matrices)arrayandanoptionalorderargument(defaultis2).Basedontheseinputsavectorormatrixnormoftherequestedorderiscomputed.Forvectorx,theorderparametercanbeanyrealnumberincludinginfor-inf.ThecomputednormisFormatrixtheonlyvalidvaluesfornormareinf,and‘fro’(or‘f’)Thus,wherearethesingularvaluesof.Examples:importnumpyasnpfromscipyimportlinalgA=np.array([[1,2],[3,4]])Aarray([[1,2],[3,4]])linalg.norm(A)5.4772255750516612linalg.norm(A,'fro')#frobeniusnormisthedefault5.47
本文标题:线性代数-python
链接地址:https://www.777doc.com/doc-4943709 .html