Import a List of Functions From a Text File in Maple

409 Views Asked by At

I have a text file that is filled by the so many functions like this

[x ^ 12 + x ^ 2 + 1 , x ^ 18 + x ^ 3 + 1 , ... , x ^ 15 + x^ 14 + x ^ 5]$\quad$ (1)

I want to use this text file in Maple software. The easy method is that I define a variable in Maple such as R and copy and past this list after R in Maple, like this

R:=[x ^ 12 + x ^ 2 + 1 , x ^ 18 + x ^ 3 + 1 , ... , x ^ 15 + x^ 14 + x ^ 5] $\quad$ (2)

and after that by the command R[i] I can access to these functions. The problem of this method is that when I past this list in Maple, the speed of evaluation the command (2) is so slow for large size of (1).

The other methods is to use commands ImportData() and readdata in Maple, but did not work. In fact, the output of these commands is not a list that I can use it (Maybe I do not know how to use these commands. I think the space between x, ^, + and numbers may cause this problem ).

My question: How to import a list of functions from a text file in Maple such that the format of output be a list of functions.

Thanks.

Edition: Based on the fabulous answer of Mr. Skoog, I want to explain my original question that made this post.

Consider the following array of functions (for large size($>10^4$) is not possible to use list) $$ \left( x+1 \right) ^{2},{x}^{3},{x}^{4}, \left( x+1 \right) \left( {x}^{2}+x+1 \right) {x}^{2}, \left( {x}^{3}+{x}^{2}+1 \right) ^{2}, \left( x+1 \right) \left( {x}^{6}+x+1 \right) , \left( x+1 \right) ^ {4},x \left( x+1 \right) ,x \left( x+1 \right) \tag{3} $$ In the rest, I need to obtain list of simple factors of this array without repetition, which mean $$ [x,x+1,{x}^{2}+x+1,{x}^{3}+{x}^{2}+1,{x}^{6}+x+1] $$ To get this, first I obtained the lcm command of this array $(3)$ as follows $$ {x}^{4} \left( {x}^{2}+x+1 \right) \left( {x}^{3}+{x}^{2}+1 \right) ^ {2} \left( {x}^{6}+x+1 \right) \left( x+1 \right) ^{4}\tag{4} $$ After that I get CodeGeneration['Matlab'] of $(4)$ which is

x ^ 4 * (x ^ 2 + x + 1) * (x ^ 3 + x ^ 2 + 1) ^ 2 * (x ^ 6 + x + 1) * (x + 1) ^ 4 $\tag{5}$

And next I copy $(5)$ in a text file and with Notepad++, I removed symbols (, ) and ^ *. Then I copy the last result in Maple.

Maybe there is a simple solution for my question and because of this I made this edition.

1

There are 1 best solutions below

5
On BEST ANSWER

I would consider importing the information as a string and then doing a few simple operations on the string. Since you didn't post the original file, I'll assume that you have a file, data.txt, that contains a series of expressions, something like:

x ^ 12 + x ^ 2 + 1 , x ^ 18 + x ^ 3 + 1 , x ^ 15 + x^ 14 + x ^ 5

You can first import the sequence using the FileTools:-Text:-ReadFile command:

input := FileTools:-Text:-ReadFile("data.txt");

This is imported to Maple as:

"x ^ 12 + x ^ 2 + 1 , x ^ 18 + x ^ 3 + 1 , x ^ 15 + x^ 14 + x ^ 5"

Next do some StringTools operations in order to make the string parseable (note that you might need to experiment with this a bit in order to make it work with your original data).

Delete all spaces:

R := StringTools:-DeleteSpace(input);

Returns:

"x^12+x^2+1,x^18+x^3+1,x^15+x^14+x^5"

Split the data by the comma delimiter:

R := StringTools:-Split(R,",");

Returns:

["x^12+x^2+1", "x^18+x^3+1", "x^15+x^14+x^5"]

Parse the data in order to make the string expressions into Maple math expressions:

R := parse~(R);

Returns:

[x^12+x^2+1, x^18+x^3+1, x^15+x^14+x^5]