I have the following code in Java, where I am trying to use the WolframAlpha command and get the result as a list of images (byte arrays)

String expr = "WolframAlpha[\"solve x^2+2x+1=0\", \"PodImages\", IncludePods->\"Result\", PodStates->\"Step-by-step solution\"]"

Evaluating the expression in a terminal gives me something like this. Using the Export function, I've seen the 2 images and they look right.

In[6]:= WolframAlpha["solve x^2+2x+1=0","PodImages", IncludePods->"Result", PodStates->"Step-by-step solution"]                                                 

Out[6]= {-Image-, -Image-}

I've tried to get the result as a byte[][] (mathLink.getByteArray2()), but I get the MLGet out of sequence exception, so I assumed the result I got wasn't an array of byte arrays.

I also tried getting the result as an Object, but I got the error that Expression on link is not a valid Java object reference

Doing mathLink.getType() told me that the result that I'm getting is a MTFUNCTION, so I tried reading the result as a function. Investigating further, I found out that the function type was List with 2 arguments. I'm assuming that those 2 arguments are the 2 images I want to get.

So my question is: How can I extract the images from the result as an array or list in Java?

Sorry if this is a silly question - I'm new to using JLink and the Wolfram Language


In case this helps anyone, I tried logging the output from mathLink.getExpr().toString() and got:

                {255, 255, 255}, {255, 255, 255}, ...more of these
        Rule[ColorSpace, "RGB"], 
        Rule[ImageSize, Automatic], 
        Rule[Interleaving, True], 
        Rule[Magnification, 1]
        ...more of the same as above

  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Nov 27 '21 at 0:37
  • $\begingroup$ I can't check it right now, but you're getting image object, not array of bytes from Mathematica (btw providing the full code you use could help) as a result of evaluate. To extract image data you can use ImageData (and in case of list of images — Map over it), something like: $\endgroup$ Nov 27 '21 at 12:09
  • $\begingroup$ 2: (this will give you a list of two arrays: ``` WolframAlpha["solve x^2+2x+1=0", "PodImages", IncludePods -> "Result", PodStates -> "Step-by-step solution"] // Map[ImageData[#, Byte, Interleaving -> True] &] // Map[Dimensions] ``` basically what math link tells you're trying to read expression (which is not an array) as an array $\endgroup$ Nov 27 '21 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.