maxima

Вопросы программирования на Free Pascal, использования компилятора и утилит.

Модератор: Модераторы

Ответить
tema
постоялец
Сообщения: 376
Зарегистрирован: 24.03.2011 19:19:27

maxima

Сообщение tema »

Кто-нибудь пробовал подключить к программе maxima для проведения символьных вычислений? Где про такое можно почитать?
tema
постоялец
Сообщения: 376
Зарегистрирован: 24.03.2011 19:19:27

Сообщение tema »

Ок, я понял, что надо попробовать другим путём. Тогда вопрос такой: как можно в паскале отправить команду в командную строку и получить и обработать вывод?
На C++ это что-то вроде:

Код: Выделить всё

> > >The simplest (and slowest) thing might be to start maxima in batch
> > >mode everytime you need to compute something, make it write the result to a
> > >file and quit.
> >
> > That sounds like a good idea, how do I start maxima in batch mode?
>
> well, from your C++ program (on solaris, or just any Unix, I guess) you can do
> roughly something like
>
> #include <stdlib.h>
> ...
> // write stuff to foo
>
> system("maxima <foo >bar"); // call maxima; hope it doesn't hang...
>
> // read bar
> ....
>
> This is of course slow (not only file I/O, but starting up maxima),
> but would give you a proof of the concept,
> so to speak:)

Here's two (not very exciting) examples. But for
non-programmers (like me!) calling Maxima with
a "system" command works well in c++, tcl/tk,
perl, quick and dirty cgi-scripting, etc.

///////////////////////////////////
// maxima_c2.cpp

#include <stdlib.h>
#include <iostream.h>

int main(int argc, char *argv[])
{
cout << "Call One Line Maxima Command from C++" << endl;
for (int i = 1; i < argc; i++)
cout << argv[i] << endl; //print args, if any

system("echo 'factor((x^2-1));' | maxima > test");
system("grep D1 test");
return 0;
}
///////////////////////////////////

# compile with
g++ maxima_c.cpp -o maxima_c

# run
./maxima_c "x^2 -1 = "

# output
Call One Line Maxima Command from C++
x^2 - 1 =
(D1) (x - 1) (x + 1)


///////////////////////////////////
// maxima_c2.cpp
// put the following commands in a file called
// "file.mac", call with "batch(file);"
//
// factor((y^2-x^2));
// expand(%);
// depends(y,x);
// diff(x^2-y^2=0,x);

#include <stdlib.h>
#include <iostream.h>

int main(int argc, char *argv[])
{
cout << "Call Maxima Batch Commands from C++" << endl;
for (int i = 1; i < argc; i++)
cout << argv[i] << endl;

system("echo 'batch(file);' | maxima > test");
system("sed -n 10~1p test");
return 0;
}
///////////////////////////////////

# compile with
g++ maxima_c2.cpp -o maxima_c2

# run
./maxima_c2 "several commands ..."

# output
Call Maxima Batch Commands from C++
several commands
2 2
(C2) FACTOR(y - x )
(D2) (y - x) (y + x)
(C3)
(C3) EXPAND(%)
2 2
(D3) y - x
(C4)
(C4) DEPENDS(y, x)
(D4) [y(x)]
(C5)
2 2
(C5) DIFF(x - y = 0, x)
dy
(D5) 2 x - 2 y -- = 0
dx

L. Prevett
Mathematics Instructor
Cochise College, Sierra Vista, AZ, US
prevettl@cochise.cc.az.us
Аватара пользователя
Дож
энтузиаст
Сообщения: 900
Зарегистрирован: 12.10.2008 16:14:47

Сообщение Дож »

Для этих целей лучше всего подходит класс TProcess
http://www.freepascal.org/docs-html/fcl ... ocess.html
Vadim
долгожитель
Сообщения: 4112
Зарегистрирован: 05.10.2006 08:52:59
Откуда: Красноярск

Сообщение Vadim »

tema
Maxima может работать в командном режиме. Ей можно передать либо строку на выполнение, если речь идёт о маленьком вычислении:

Код: Выделить всё

maxima --very-quiet --batch-string="sin(%pi/4)"

либо готовый файл с командами, если речь идет о большом объёме:

Код: Выделить всё

maxima --very-quiet --batch=proga.mac

и вывод ловить, если используете TProcess, в TProcess.Output.

Есть ещё оболочка над TProcess - функция RunCommand. ;-)
tema
постоялец
Сообщения: 376
Зарегистрирован: 24.03.2011 19:19:27

Сообщение tema »

Большое спасибо!
daesher
постоялец
Сообщения: 221
Зарегистрирован: 09.03.2010 21:17:14

Сообщение daesher »

Пробовал. Вот парочка модулей (sets и mystr нужны для всего проекта, но модули на них завязаны, поэтому прикладываю)
У вас нет необходимых прав для просмотра вложений в этом сообщении.
tema
постоялец
Сообщения: 376
Зарегистрирован: 24.03.2011 19:19:27

Сообщение tema »

daesher писал(а):Пробовал. Вот парочка модулей (sets и mystr нужны для всего проекта, но модули на них завязаны, поэтому прикладываю)

Спасибо!
Сейчас буду смотреть :)
Ответить