高校化学2924392 views
いろは3010814 views
高校生物551766 views
MathPython497343 views
LaTeX962153 views
中学英語811710 views
Computer368106 views
りんご209490 views
高校物理160203 views
ヒストリア290598 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 で数式を扱うには、まずシンボル(記号変数)を定義する必要がある。シンボルは数学の に相当するものだ。

symbols 関数

symbols 関数でシンボルを定義する。

from sympy import symbols

x = symbols('x')
y = symbols('y')

複数のシンボルを一度に定義することも可能だ。

x, y, z = symbols('x y z')

カンマ区切りでも動作する。

a, b, c = symbols('a, b, c')

Symbol クラス

Symbol クラスを直接使う方法もある。

from sympy import Symbol

x = Symbol('x')

symbols は内部で Symbol を呼び出しているため、機能的には同じだ。複数定義する場合は symbols のほうが簡潔に書ける。

式の作成

シンボルを定義したら、通常の演算子で式を組み立てられる。

from sympy import symbols

x, y = symbols('x y')

expr1 = x + y
expr2 = x * y
expr3 = x ** 2 + 2*x + 1

SymPy の関数を使った式も作れる。

from sympy import symbols, sin, cos, exp, log

x = symbols('x')

expr1 = sin(x) + cos(x)
expr2 = exp(x) * log(x)

シンボルの属性

シンボルに属性を付けることで、計算結果が変わる場合がある。

from sympy import symbols, sqrt

x = symbols('x', positive=True)
sqrt(x**2)  # x(正なので絶対値が不要)

y = symbols('y')
sqrt(y**2)  # sqrt(y**2)(符号不明なので簡約化されない)
positive=True

シンボルが正の値であることを指定。平方根の簡約化などに影響する。

real=True

シンボルが実数であることを指定。複素数を含む計算で結果が変わる。

integer=True

シンボルが整数であることを指定。整数性を利用した簡約化が行われる。

属性を適切に設定すると、より簡潔な結果が得られることがある。