Java / groovy库以代数方式求解变量

时间:2012-02-02 09:50:36

标签: java groovy algebra

如果我有一个方程y = 3x我可以用“代数”来得到方程x = y / 3。有什么我能给的东西:

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

我认为JScience可以做到这一点,但我似乎无法弄清楚它是否存在。

2 个答案:

答案 0 :(得分:0)

我认为WolframAlpha API可以做到这一点,在资源管理器中尝试类似的东西http://products.wolframalpha.com/api/explorer.html

y = 3x; y=6; x?

答案 1 :(得分: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 { it.@title.text() == "Solution for the variable x" }.toString()

返回:x = f/3

注意:如果您需要处理实际的关闭代码,请检查print the closure definition/source in Groovy