Group Abstract Group Abstract

Message Boards Message Boards

Exporting and convertion to prefix

GROUPS:
Hi,

I have 2 problems.

1- I was trying to export data into the format of a table or list, which is very confusing. I have a list of solutions after solving matrices and each list has a set of solutions. Is there any way that I can arrange and export data in a way that it is feasible to parse or import to any other language C# for instance. The export format is not structured so I am confused how to parse them to c# or C++. following is the example.
{k[1][3] == -4932.697781948424 - 1.156160458452722*k[1][1] - 0.11882000520969001*k[1][2], k[2][3] == -506.9393022948684 - 1.156160458452722*k[2][1] - 0.11882000520969001*k[2][2], k[3][3] == -4266.4474 - 1.156160458452722*k[3][1] - 0.11882000520969001*k[3][2]}
{k[1][3] == 44.77069128837509 + 1.4630700064226076*k[1][1] - 7.143808022420739*k[1][2], k[2][3] == -218.6041831157821 + 1.4630700064226076*k[2][1] - 7.143808022420739*k[2][2], k[3][3] == -30.600512000000005 + 1.4630700064226076*k[3][1] - 7.143808022420739*k[3][2]}
{k[1][3] == -0.00006917344248542953 - 0.00013834680030261727*k[1][1] - 0.0006715633871188116*k[1][2], k[2][3] == -0.0003357818990578023 - 0.00013834680030261727*k[2][1] - 0.0006715633871188116*k[2][2], k[3][3] == -0.500000306 - 0.00013834680030261727*k[3][1] - 0.0006715633871188116*k[3][2]}


2- Is there any way that I can convert my solutions into prefix form?

Is there any way to deal with both of the issues.
POSTED BY: Usman Rauf
Answer
7 months ago
If you are importing something into a different programming language, you probably want to export your data in a defined format, like CSV or XLS for example. To do this, you will first need to pick a data format. Then you will have to convert the output into a grid or list of numbers. 

Currently you have a list of equations. There isn't a standard way of representing symbolic equations across different programming languages. If you want to save data in Mathematic and import it in C# or C++, you will need to convert it to a list of numbers, export it from Mathematica, import it in the new programming language, and then reinterpret the data correctly there.

2. What is prefix form? Are you talking about Polish notation?
POSTED BY: Sean Clarke
Answer
7 months ago
1- What do you suggest me to convert it to a specific data format?

2- Yes I am refering to Polish notation...
POSTED BY: Usman Rauf
Answer
7 months ago
1. The hard part of this is writing the C++/C# code to translate whatever you import into a form that you can use.

How will you represent these equations in C++/C#?

This is the important question. I can't choose a data format for you because I don't know how you want to represent the equations in those languages.

2. If you run Mathematica expressions through FullForm,  you'll see that they are in something like Polish notation. You can turn this into a string and then program some kind of string manipulation. Depending on the complexity of the problem, you would effectively be writing a parser.
POSTED BY: Sean Clarke
Answer
7 months ago
I just want to consider them as a equation in c#.  such as the following

k[1][3] == -4932.697781948424 - 1.156160458452722*k[1][1]

in to:

k13 = -4932.697781948424 - 1.156160458452722* k11

problem is how to convert k[1][1] into k11. In mathematica k[1][1] is part of an array but in other language I cant take it as it is. The question is how to replace all variables in this solution which are in the form of k[1][1] into k11 as a variable. After that my intention is to pass it to a constraint solver.

I did not get the IDEA of using FullForm to convert infix to prefix. Can you elaborate little more?
POSTED BY: Usman Rauf
Answer
7 months ago
What is k11 going to be in your C# code? This code:
k13 = -4932.697781948424 - 1.156160458452722* k11
doesn't make sense in C# unless k11 is something can be multiplied and added with numbers. C# doesn't have equations as a fundamental type. Can you be more specific about what you mean when you say you want to make the above code into an equation in C#? I think the misunderstanding here is mostly conceptual. 

Transforming k[1][1] into the variable k11 in Mathematica won't be difficult. This is essentially the code that does this as a replacement rule. 
k[1][1] /. k[n_][m_] :> Symbol["k" <> ToString@n <> ToString@m]
You can run the replacement on a larger expression instead of just "k[1][1]" of course. 



Try running FullForm on one of your equations :
FullForm[k[1][3] == -4932.697781948424 - 1.156160458452722*k[1][1]]
Equal[k[1][3], Plus[-4932.697781948424`, Times[-1.156160458452722`, k[1][1]]]]


Once you have it in this form, you'll want to use string manipulation. I guess I would do something like:
 myStringExpression =
  ToString@
 FullForm[
   k[1][3] == -4932.697781948424 - 1.156160458452722*k[1][1] /.
     k[n_][m_] :> Symbol["k" <> ToString@n <> ToString@m]
 ];
 
 StringReplace[myStringExpression,
 {"Equal" -> "==", "Plus" -> "+", "Times" -> "*", "," -> "", "[" -> "", "]" -> ""}]

Which gives:
"==k13 +-4932.697781948424` *-1.156160458452722` k11"
POSTED BY: Sean Clarke
Answer
7 months ago
Great this seems good though If I can write an out put file in a specific format I dont even need to go through the hectic process of making a parser in C#.
But after converting to prefix the new issue is to right it into a specific format. e.g. the following
==k13 +-4932.697781948424` *-1.156160458452722` k11
should be
(assert (= k13 (+ -4932.697781948424 ( * -1.156160458452722  k11) )  ))

which means every sub expression should be seperated by "()" so that compiler can understand which parts are seperated. Is there any way to write file ? according to some rules as you mentioned before in StringReplace command ?
{"Equal" -> "==", "Plus" -> "+", "Times" -> "*", "," -> "", "[" -> "", "]" -> ""}]

In simple Can I make a parser or can I define gramer? my main goal is to convert the resut into this context:
(assert (= k13 (+ -4932.697781948424 ( * -1.156160458452722  k11) )  ))
I tried writing a file via opening a stream or channel at a specific location but I am failed so far it seems there is no way to creat/access a file for writing other then "Export command": I used following but I am unable to access the file stored at a specific location.
In[10]:= ftest = FileNameJoin[{$Mathematica Solvings, "Test"}]

Out[10]= FileNameJoin[{Solvings $Mathematica, "Test"}]
Although it gives me no error but when I tried to acess this file I received error:
In[11]:= test = OpenWrite[ftest]

During evaluation of In[11]:= OpenWrite::fstr: File specification FileNameJoin[{Solvings $Mathematica,Test}] is not a string of one or more characters. >>

Out[11]= $Failed
POSTED BY: Usman Rauf
Answer
7 months ago