2

I want to load an external Magma file within another Magma file. (Both files are saved in the same directory.) I want to be able to quickly change which external file is being loaded, ideally at the beginning of the file making the load call, so that I can easily run the same code with various inputs.

(The external file contains computations, whose ultimate result is used by the file making the load call. These computations vary depending on the object being analyzed.)

I tried creating a string-type variable that stores the external file's name, then using Magma's load command with this variable. For example,

fileName := "externalMagmaFile.txt";
load fileName;

However, this results in the error

User error: Could not open file "fileName" (No such file or directory)

The same error results when I include double quotes around the external file name:

fileName := "\"externalMagmaFile.txt\"";
load fileName;

It seems that, for the load command, Magma interprets the variable name as the string specifying the file name, instead of first evaluating the variable, then executing load.

(I am using Magma V2.23-1 on MacOS Version 10.15.5.)

Can I use a variable with the load command in Magma? If yes, how?

ev.gal
  • 121
  • 1
    OK, this is a very inelegant solution, but one way to do it would be to do a system call inside Magma:
    1. Tell Magma to make a file "dummyloadfile".
    2. Tell Magma to write "load "realfile";" into dummyloadfile.
    3. Execute the command load "dummyloadfile" in your text file.

    This is awful, but might work.

    – David A. Craven Jun 30 '20 at 22:13
  • @DavidCraven : I confirm the proposed method works. Thank you!

    Unfortunately, when I use this approach in my full code, Magma returns User error: bad syntax. This may be related to the load command being inside an if statement; see this (unanswered) post on Stack Overflow. In my case, Magma seems to ignore code within the if statement preceding the load command, including the creation of the dummy load file.

    – ev.gal Jul 02 '20 at 19:26
  • Yes. It's incredibly annoying but you cannot use load commands inside if statements. I wanted to do this myself as well on several occasions to only load one of a number of different data files. – David A. Craven Jul 02 '20 at 20:34
  • I was going to suggest the eval command, but you can only use it with expressions and not statements... – xxxxxxxxx Jul 10 '20 at 01:25
  • Really for this problem (you have computations that differ depending on the type of object in question), you should write an overloaded intrinsic (so you have several versions of the intrinsic that differ in the type of objects they take as argument, and run the appropriate computations accordingly). I'll try to put up an example of this later this evening. – xxxxxxxxx Jul 10 '20 at 01:29
  • @MorganRodgers : In my particular application, I'm trying to load different versions of (1) commands defining field extensions and performing related computations, and (2) lists of elements (corresponding to curves on a surface). The field extensions and lists depend on an original object, which is always a set of equations defining a surface. After (1) and (2) are specified, subsequent analysis is the same. – ev.gal Jul 12 '20 at 17:07
  • @ev.gal Even then, you can define a function or intrinsic FEFunctions that uses conditionals to build the appropriate functions, and it can return the appropriate functions. So if you have functions FE1 and FE2 that might behave differently depending on properties of the list L, FE1, FE2 := FEFunctions(L) can decide which functions are appropriate and return them. – xxxxxxxxx Jul 12 '20 at 18:34
  • The key to remember is that functions are first class objects, and can be passed along and renamed as needed. Having a function build other functions for you is probably more in line with what Magma wants you to do (as the load command is mostly kind of awkwardly implemented). – xxxxxxxxx Jul 12 '20 at 18:37
  • This question is off-topic here. –  Jan 13 '23 at 09:08

2 Answers2

0

Another solution (which is also a bit of a work around) is to set the Magma path based on which file you want to load using the SetPath() function. The Magma path indicates which directories are searched for loading Magma files.

To clarify, if you have 2 different files by the same name in separate directories (or can set it up so that you do) and want to choose which one to load, you can set filepath := "directory1" or filepath := "directory2" and then SetPath(filepath) before loading the file.

If you are concerned about changing the path, you can use GetPath() to save off the previous one and restore afterwards. Or add the new path to the end of the current path before calling SetPath().

Side note: I would have added this as a comment rather than an answer since it is a workaround and it is not the ideal solution for every scenario. But adding comments requires reputation, and I came upon this question because I was in a similar situation and this answer would have helped me. In my case I wanted to load several files from the same path (where the path is a variable determined at runtime) so this worked better for me than trying to do a dummyloadfile for each. Also, sadly, the true answer to your question (afaik) is that there is no direct way to load a file using a variable name.

kera
  • 1
-1

Here is sample code illustrating the approach proposed by @DavidCraven. (The file realFile.txt contains the Magma commands we desire to load.)

fileNameReal := "realFile.txt";
PrintFile("dummyLoadFile.txt","load \"" cat fileNameReal cat "\";" : Overwrite := true);
load "dummyLoadFile.txt";
ev.gal
  • 121