Message Boards Message Boards

See intermediate results of a function

Posted 1 year ago

Hello,

I have to migrate a code from Mathematica to python and I would like to know which are the functions that I could use the print the results in the nested functions.

POSTED BY: Daniela Tortora
13 Replies

I understand, no problem, you did much more that what expected, since there are some codes written from other people I cannot copy and past it as it is, for this reason I tried to modify it a little I will try to find a way to print the results and to export them but in the meanwhile I try to start to write the code in Python Many thanks for your help

POSTED BY: Daniela Tortora
Posted 1 year ago

I feel like I set expectations that I could just "do it for you", but we are clearly having difficulty. Also, it turns out, I'll be largely offline for the next week. So, I apologize for overpromising. If you can wait for awhile, and if you can find the actual syntactically correct running code, then maybe we can work this out later. But I'm starting to wonder if it wouldn't be easier for you to just implement the functionality directly in Python from scratch.

POSTED BY: Eric Rimbey

I am very sorry, I had to modify the names of the variables. The code is longer and wanted to bother you as less as possible so I copied only few steps of it. Hope that now it works. Many thanks, I couldn't imagine that there were people that could spend their time to help me Many many thanks

POSTED BY: Daniela Tortora
Posted 1 year ago

This line

M = Import[NotebookDirectory[] <> "file2.xlsx", {"Sheets", "Sheet1"}]

means this is dependent on a file, and obviously I don't have that file.

And there is still something wrong with the last function definition, the one for f[tarB_, tarG_, RhoBG_].

Have you actually successfully run any of this code? If so, something is getting lost between copying it from the notebook to pasting it in this forum.

POSTED BY: Eric Rimbey

Thank you very much! the code is not long, I need to see the results evaluated and export the results.

POSTED BY: Daniela Tortora
Posted 1 year ago

If that's the entire code, then we have some problems. There are many undefined variables:

`dist`, `dB`, `dG`, `iJDP`, `K`, `t`, and `B`

The product t B is probably an error and was intended to be a single symboltB.

There is also a syntax problem in the definition of f. I'm not exactly sure what is being attempted, so I don't know if it's just missing brackets or missing lines of code.

So, I can't even get this to run properly.

POSTED BY: Eric Rimbey

I have also another question Eric, the output of print shows me no the numerical values but something like this RhoLG$19861474, do you know why?

POSTED BY: Daniela Tortora
Posted 1 year ago

The variable RhoLG was declared inside the Module. Mathematica uses a re-naming scheme to scope variables, so inside the Module the variable that looks to you like RhoLG is really RhoLG$19861474.

(The bit after the $ will be different each time the Module executes.

But, if you put the Print statement inside the Module it should work fine. So, you must have been putting the Print outside the Module. Since you want intermediate results, put the Print right next to each place that RhoLG is defined or updated.

If you're still having trouble, I'll try to put together a demo. But I don't have much time right now, so it'll have to wait until later (or maybe someone else can jump in and help you out).

POSTED BY: Eric Rimbey
Posted 1 year ago

Okay, just getting back to this. Ignore my previous post. I was trying to debug in my head without taking time to think it through. I don't think what I said actually applies. How big is the notebook that you're trying to port to Python? It sounds like you're not very comfortable with manipulating Mathematica code, and it would be much easier to just show you how to do it rather than give you descriptions or dummy code. If the notebook isn't too large, just upload it and I'll try to work directly on it. Or at the very least provide a complete implementation for the f function.

POSTED BY: Eric Rimbey

Many thanks Eric, About the intermediate results, so for instance a step code that extract the RhoLG how should it be?' I am not very confident with the syntax of Mathematica and I would like to extract the intermediate results to compare them with the ones in Python

POSTED BY: Daniela Tortora
Posted 1 year ago

If you want to export these intermediate values, you're going to need a structure that gathers the data as your evaluation goes on. One way would be to replace each Print you just added with Sow. Then surround the whole evaluation with Reap.

But are we still talking about intermediate results, or are you trying to export actual final results?

POSTED BY: Eric Rimbey

Many many thanks!!!!!!!!!! And if I would like to export as xlsx or csv or txt the RhoLG which step should I create? Still many thanks!!

POSTED BY: Daniela Tortora
Posted 1 year ago

You could just add Print statements. Make sure you add semi-colons as necessary, but you can add Print pretty much anywhere you want. The form could be something like

Print["RhoLG = ", RhoLG];
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract