Group Abstract Group Abstract

Message Boards Message Boards

Solve numericizes after turning mathematica off and on again

(* Open an input box to name the file group *)
GroupName = InputString["Enter the file group name:"];
(* Open an input box to name the folder *)
FolderName = InputString["Enter the folder name on the desktop:"];
(* Determine the desktop path *)
DesktopPath = FileNameJoin[{Environment["USERPROFILE"], "Desktop", FolderName}];
(* Create the folder if it doesn't exist *)
If[!DirectoryQ[DesktopPath], CreateDirectory[DesktopPath]];

(* Set the directory as trusted *)
SetDirectory[DesktopPath];

ClearAll
Begin["restrictedContext`"]
Context[dd]
(* Import the variables *)
jet4 = Import["zeros.txt"]; jet = ToExpression[jet4];
jotit = Import["final.txt"];
minus = Import["zerosverdades.txt"];
minus1 = ToExpression[minus];
let2 = jet;
gt = let2 - minus1;
let22 = let2 - gt;
let2 = ToExpression[let22];
jotitvv3 = StringJoin["1.", StringJoin[Table["0", {jet}]], jotit];
vvv = ToString[jotitvv3];
ee3 = ToExpression[vvv]
ii31 = Import["ii3.txt"];
ii3 = ToExpression[ii3];
ii41 = Import["ii4.txt"];
ii4 = ToExpression[ii4];
ii21 = Import["ii2.txt"];
ii2 = ToExpression[ii2];
hh = Import["gh.txt"];
hh2 = ToExpression[hh];
ee3 = dd;
question = {x} /. Solve[(ii2*ii3*x + ii4)/(ii2*ii3*x) == dd, x]
N[%];
send = ii2*ii3*question + ii4;
send2 = Flatten[send];
send3 = Flatten[send2];
answer = DecimalForm[send3];
a = {x1} /. Solve[(ii2*ii3*x1 + ii4)/(ii2*ii3*x1) == dd, x1];
dd = ee3;
letters = (ii2*ii3*a)*dd;
letters1 = ToString[letters];
digits = IntegerDigits[letters, 256];
digits2 = Flatten[digits];
gh = hh2;
hj = Take[%, -1];
bb = Partition[digits2, 4];
bg = Partition[bb, hj];
image = Image[bg, "Byte", ColorSpace -> "RGB", ImageSize -> gh];
img = image
labImg = ColorConvert[img, "LAB"]

(*Exporting ii2, ii3, ii4 with custom names*)
Export[desktoppath <> "/" <> groupname <> "_gh.txt", hh];
Export[desktoppath <> "/" <> groupname <> "_zeros.txt", jet4];
Export[DesktopPath <> "/" <>GroupName <> "_final.txt", jotit];
Export[DesktopPath <> "/" <>GroupName <> "_truezeros.txt", minus];
Export[DesktopPath <> "/" <>GroupName <> "_ii2.txt", ii2];
Export[DesktopPath <> "/" <>GroupName <> "_ii3.txt", ii3];
Export[DesktopPath <> "/" <>GroupName <> "_ii4.txt", ii4];
Save[DesktopPath <> "/fileSaved.mx", {hh, jet4, jotit, minus, ii2, ii3, ii4}];

End[]

(* Restore the previous directory *)
ResetDirectory[];

If I run the decoder above it works perfectly if i do not turn off mathematica after I generate via encoder the files ii and the others needed ... but if i turn it off and import the files that are imported just fine the command solve does not give me the right answer as before... could anyone please resolve this issue ?

6 Replies

You're saving variables and definitions, but not the source code of the packages loaded with Needs. Needs searches for .m or .wl files on the system and loads their contents. These files aren't included in the saved state—only the symbols and values that were in memory.

So, even if you used XMPTools functions in the encoder, the decoder can't use them after restarting the kernel because the original package code is no longer available. My question then is why i can not load de packages opened when i used the encoder before the decoder ? with needs Needs["XMPTools"]; Needs["Utilities"]; Needs["Interpreter"]; Needs["CloudObject"]; Needs["Authentication`"]; that seem to be necessary for the decoder above to function properly, as they are present after i run the encoder and vanishes after I turn off the mathematica kernel

The problem isn't with the Solve function itself, but with the lack of access to the contexts and packages needed to process the input data (ii2, ii3, ii4) after the kernel restarts. You've accurately identified that the failure occurs because the encoder execution initializes a kernel state that isn't persistent. When you use Quit[], the kernel is completely restarted, clearing all definitions, variables, and, crucially, external packages that were implicitly loaded during the encoder execution. Why aren't the packages loaded? Mathematica loads many packages and contexts on demand. If your extraction function uses, for example, URLRead (which is in UtilitiesURLTools), that package is automatically loaded the first time the function is called. However, the decoder has no prior knowledge of this. It attempts to use the data (ii2, ii3, ii4`), which, at this point, is either undefined or has incorrect values due to the failed import/processing step. If the encoder extracts data like TextRecognize[image], TextRecognize is in the System context and usually works without problems. However, if the extraction depends on packages like XMPTools, which are not part of the main context, it will fail after a kernel restart. Solution: Making the decoder independent The best approach is to ensure that the decoder is a "standalone" program that doesn't depend on the state of a previous session. This means including all the code necessary to reproduce the environment and input data. Here are the workaround steps, focusing on resolving the "package upload" issue: Step 1: Identify and list the packages You've already done a great job identifying the likely packages. Your list is a great starting point. Most of these packages are standard in the Mathematica installation. You don't need to "upload" them; they're already there, they just need to be loaded into the current session. The Needs["Context"] command does exactly that: it checks whether the context is loaded and, if not, attempts to load it from the installed packages. Step 2: Add Needs to the decoder Modify the decoder code so that it explicitly loads the required packages before attempting to process the data.

I think that ClearAll by itself, without arguments, achieves nothing.

Also, IntegerDigits acts on numbers, not strings.

POSTED BY: Gianluca Gorni

This appears to require auxiliary files to reproduce.

POSTED BY: Daniel Lichtblau
hh = "{225, 225}"

jet4 = "487663"

jotit = "1407855"

minus = "4000"

ii2 = 500

ii3 = {706}

ii4 = {160047}
hh = "{225, 225}"

jet4 = "487663"

jotit = "1407855"

minus = "4000"

ii2 = 500

ii3 = {706}

ii4 = {160047}
hh2 = {225, 225}

jet4 = "487663"

jotit = "1407855"

minus = "4000"

If you can not resolve this who will ??? congrats to your curriculum!!!! thanks in advance

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard