2012-02-02 36 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

前一段時間我做了Wolfram Alpha的API爲denis.solonenko類似的話(實際上是一個快速&髒測試)提示。要實現它,請使用「f = 3 * x」,因爲api響應有點不同,並且包含變量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如果你需要處理的實際關閉代碼。