小学理科719870 views
りんご209490 views
高校日本史190571 views
雑学1473612 views
世界の国564520 views
LaTeX962153 views
MathPython497343 views
中学英語811710 views
小学算数1200427 views
中学理科1630641 views
Help
Tools
NewsSpreadsheetCalendarSlidesTier ListPen ToolIllustrationCrayonPixel ArtASCII ArtPerspectiveEndless StairsGraphMind MapER DiagramFamily TreeMemeCurved TextImage EditorMosaicRetro FilterPencil SketchOCR/HighlighterMakeup EditorFaviconVideo TrimmerScrolling VideoVideo TitleColor PickerColor ExtractorBonfireFireworksWater RippleWater SplashBreaking GlassWood GrainMarble TextureCSS ButtonIcon MakerBar ChartGrouped Bar ChartStacked Bar ChartPie ChartLine ChartArea ChartStacked Area ChartScatter Plot3D Bar Chart3D Pie ChartBar Chart RaceBubble ChartPopulation PyramidPictogramEarningsCandlestick ChartInvestment RiskMortgage SimulatorCalculatorMatrix CalculatorFunction GraphPolynomial ExpansionVenn DiagramField VisualizerRubik's Cube Group TheoryTraveling SalesmanVoronoi and DelaunayFractalColumn ArithmeticDraw Math FiguresArithmetic AnimationArithmetic Word ProblemsCounting with Tree DiagramsCube NetsCross SectionsMotion PathMechanicsWavesLight and LensesThermodynamicsHow Semiconductors WorkMolecular StructuresAtomic OrbitalsElectrochemical CellsChemical EquilibriumCrystal LatticesBuffer pHComplex IonsDNA Double HelixCell DivisionMembrane ChannelsNerve ImpulseRock ClassificationWeatherConstellationsSolar and Lunar Eclipses3D ModelingFloor PlanSeismic StructuresIntersection TurnMaglevCooking AnimationOrigamiLive Viewer CountGeoJSON MapRailway MapPopulation MapCrime MapLand Price MapSchool MapShrine and Castle MapHouse of Representatives MapWord MapSolitaireReversiHakoiri MusumeChessHamburgerRippleSlide Puzzle MakerNeon PinballNovel MakerJapanese Typing PracticePiano Score EditorMusic Theory

English

SymPy とは?数式処理の基本

SymPy は Python で数式処理を行うためのライブラリだ。数値計算ではなく、数式そのものを扱える点が特徴である。

数式処理とは

通常の Python で計算すると、結果は数値になる。

import math
math.sqrt(8)  # 2.8284271247461903

一方、SymPy を使うと数式のまま扱える。

from sympy import sqrt
sqrt(8)  # 2*sqrt(2)

という形式で返ってくる。これが数式処理の基本的な考え方だ。

インストール

pip でインストールできる。

pip install sympy

基本的な使い方

SymPy では、まずシンボル(変数)を定義し、それを使って式を組み立てる。

from sympy import symbols, expand

x = symbols('x')
expr = (x + 1)**2
expand(expr)  # x**2 + 2*x + 1

シンボルを定義

式を組み立てる

関数で処理

NumPy との違い

NumPy は数値計算に特化しており、配列演算が高速に行える。SymPy は数式の厳密な操作に向いている。

NumPy

数値計算向け。配列を高速に処理し、近似値を返す。

SymPy

数式処理向け。式を厳密に展開・簡約化し、記号のまま結果を返す。

用途に応じて使い分けるのがよい。数値シミュレーションなら NumPy、式変形や証明の補助なら SymPy が適している。