2012-02-02 3 views
1

私が方程式y = 3xを持つならば、 "代数"を使って方程式x = y/3を作ることができます。代数的に変数を解くためのJava/groovyライブラリ

def y = {x-> x*3} 
def x = ThisWouldBeNice.solveForMe(y, 'x') //does the same as: def x = {y-> y/3} 

私はJScienceこの操作を行うことができると思ったが、私はそのそこにあればそれを把握するように見えることはできません。私は次のように与えることができるものがあります。

答えて

0

いくつかの時間前、私はやったdenis.solonenkoとしてウルフラムアルファのAPIと似たような(実際には、迅速&汚いテスト)示唆している。これを実現するには、「f = 3 * x」を使用します.pi応答は少し異なり、変数xの解も含まれています。

このコードには、レスポンスのコピー(正しいAPI IDを使用して直接取得する)が含まれています。

import java.net.URLEncoder 

def stringEquation = "f = 3 * x" 
def equation = { x -> 3*x } 

//def response = "http://api.wolframalpha.com/v2/query?appid=xxx&input=" + URLEncoder.encode(stringEquation) + "&format=plaintext".toURL().text 

def response= """<?xml version='1.0' encoding='UTF-8'?> 
<queryresult success='true' 
    error='false' 
    numpods='6' 
    datatypes='Geometry' 
    timedout='' 
    timing='0.766' 
    parsetiming='0.181' 
    parsetimedout='false' 
    recalculate='' 
    id='MSP6141a0482cfc786eibg000036bb0i3bhf6d6aih&amp;s=50' 
    related='http://www4a.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSP6151a0482cfc786eibg00005a4g16ee5ei232bf&amp;s=50' 
    version='2.1'> 
<pod title='Input' 
    scanner='Identity' 
    id='Input' 
    position='100' 
    error='false' 
    numsubpods='1'> 
    <subpod title=''> 
    <plaintext>f = 3 x</plaintext> 
    </subpod> 
</pod> 
<pod title='Geometric figure' 
    scanner='Geometry' 
    id='GeometricFigure (ofBoundary)' 
    position='200' 
    error='false' 
    numsubpods='1'> 
    <subpod title=''> 
    <plaintext>line</plaintext> 
    </subpod> 
    <states count='1'> 
    <state name='Properties' 
     input='GeometricFigure (ofBoundary)__Properties' /> 
    </states> 
</pod> 
<pod title='Plot' 
    scanner='Plotter' 
    id='Plot' 
    position='300' 
    error='false' 
    numsubpods='1'> 
    <subpod title=''> 
    <plaintext></plaintext> 
    </subpod> 
</pod> 
<pod title='Alternate form' 
    scanner='Simplification' 
    id='AlternateForm' 
    position='400' 
    error='false' 
    numsubpods='1'> 
    <subpod title=''> 
    <plaintext>f-3 x = 0</plaintext> 
    </subpod> 
</pod> 
<pod title='Solution for the variable x' 
    scanner='Reduce' 
    id='SolutionForTheVariableV' 
    position='500' 
    error='false' 
    numsubpods='1' 
    primary='true'> 
    <subpod title='' 
     primary='true'> 
    <plaintext>x = f/3</plaintext> 
    </subpod> 
</pod> 
<pod title='Implicit derivatives' 
    scanner='ImplicitDifferentiation' 
    id='ImplicitDerivatives' 
    position='600' 
    error='false' 
    numsubpods='2'> 
    <subpod title=''> 
    <plaintext>(dx(f))/(df) = 1/3</plaintext> 
    </subpod> 
    <subpod title=''> 
    <plaintext>(df(x))/(dx) = 3</plaintext> 
    </subpod> 
    <states count='1'> 
    <state name='More' 
     input='ImplicitDerivatives__More' /> 
    </states> 
</pod> 
</queryresult>""" 

def queryresult = new XmlSlurper().parseText(response) 
def solution = queryresult.pod.findAll { [email protected]() == "Solution for the variable x" }.toString() 

それを返します:x = f/3

注:実際の閉鎖コードに対処する必要がある場合print the closure definition/source in Groovyをご確認ください。

関連する問題