Recent

Author Topic: Calling a Maple program in lazarus pascal  (Read 811 times)

sedsil

  • New Member
  • *
  • Posts: 11
Calling a Maple program in lazarus pascal
« on: October 17, 2025, 08:57:24 pm »
I have a Lazarus Pascal program. One of the modules inside the program does not work very well, or the digit accuracy is not sensitive enough. So, I want to use a Maple module written inside the Pascal program. Each time Pascal calls it in the program, the output will be used. Could someone provide a small program example? If so, that would be great. By the way, there is no need to write the Maple output in the text file or anywhere else. It will be used directly.
I would like to explain using an example. In Maple, assume that we have a second-degree equation such that f(a, b, c) = a x² + b x + c. When we run Pascal, we transfer a, b and c from Lazarus Pascal to Maple. Once Maple has finished, it will send the roots back to Pascal. If you could write a short code, it would be easier to apply it to my real problem.



 Thank you in advance
Sedsil
« Last Edit: October 21, 2025, 02:51:57 pm by sedsil »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8831
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Calling a Maple program in lazarus pascal
« Reply #1 on: October 27, 2025, 04:56:34 pm »
Unfortunately, there's no ready to use OpenMaple API for Pascal or Delphi (which we should be able to adjust lightly). So you will have to make one in the first place, converting from C. You don't need to convert everything, possibly only:
  • StartMaple
  • StopMaple
  • EvalMapleStatement
  • ALGEB and M_ENV data types
  • One or more MapleToXXX
EvalMapleStatement accepts a string, you can write the second degree equation as is there.

LV

  • Sr. Member
  • ****
  • Posts: 359
Re: Calling a Maple program in lazarus pascal
« Reply #2 on: October 27, 2025, 11:08:44 pm »
This could be helpful: Calling Maxima from a Pascal program.  :-[

Code: Pascal  [Select][+][-]
  1. program MaximaSolveDemo;
  2.  
  3. uses
  4.   SysUtils, Classes, Process;
  5.  
  6. function RunMaximaOnce(const Expr: string): string;
  7. var
  8.   P: TProcess;
  9.   Output: TStringList;
  10. begin
  11.   P := TProcess.Create(nil);
  12.   Output := TStringList.Create;
  13.   try
  14.     P.Executable := 'C:\maxima-5.46.0\bin\maxima.bat';
  15.     P.Parameters.Add('--very-quiet');
  16.     P.Parameters.Add('-r');
  17.     P.Parameters.Add(Expr + '; quit();');
  18.     P.Options := [poWaitOnExit, poUsePipes];
  19.     P.Execute;
  20.     Output.LoadFromStream(P.Output);
  21.     Result := Output.Text;
  22.   finally
  23.     Output.Free;
  24.     P.Free;
  25.   end;
  26. end;
  27.  
  28. function SolveQuadratic(a, b, c: Double): string;
  29. var
  30.   cmd, outstr: string;
  31.   FmtSettings: TFormatSettings;
  32. begin
  33.   FmtSettings := DefaultFormatSettings;
  34.   FmtSettings.DecimalSeparator := '.';
  35.   cmd := Format('solve(%.2f*x^2 + %.2f*x + %.2f = 0, x)', [a, b, c], FmtSettings);
  36.   outstr := RunMaximaOnce(cmd);
  37.   Result := Trim(outstr);
  38. end;
  39.  
  40. function SymbolicOperation(const Operation: string): string;
  41. var
  42.   outstr: string;
  43. begin
  44.   outstr := RunMaximaOnce(Operation);
  45.   Result := Trim(outstr);
  46. end;
  47.  
  48. procedure DemoSimplification;
  49. begin
  50.   Writeln('Expression Simplification:');
  51.   Writeln('(x + 1)^3 = ', SymbolicOperation('expand((x + 1)^3)'));
  52.   Writeln('(x^2 - 1)/(x - 1) = ', SymbolicOperation('ratsimp((x^2 - 1)/(x - 1))'));
  53.   Writeln;
  54. end;
  55.  
  56. procedure DemoDifferentiation;
  57. begin
  58.   Writeln('Differentiation:');
  59.   Writeln('d/dx(sin(x)) = ', SymbolicOperation('diff(sin(x), x)'));
  60.   Writeln('d/dx(x^3 + 2*x^2 + x) = ', SymbolicOperation('diff(x^3 + 2*x^2 + x, x)'));
  61.   Writeln('d^2/dx^2(x^3 + 2*x^2 + x) = ', SymbolicOperation('diff(x^3 + 2*x^2 + x, x, 2)'));
  62.   Writeln;
  63. end;
  64.  
  65. procedure DemoIntegration;
  66. begin
  67.   Writeln('Integration:');
  68.   Writeln('integral of x dx = ', SymbolicOperation('integrate(x, x)'));
  69.   Writeln('integral of x^2 + 2*x + 1 dx = ', SymbolicOperation('integrate(x^2 + 2*x + 1, x)'));
  70.   Writeln('integral of sin(x) dx = ', SymbolicOperation('integrate(sin(x), x)'));
  71.   Writeln;
  72. end;
  73.  
  74. procedure DemoLimits;
  75. begin
  76.   Writeln('Limits:');
  77.   Writeln('lim(x->0) sin(x)/x = ', SymbolicOperation('limit(sin(x)/x, x, 0)'));
  78.   Writeln('lim(x->inf) 1/x = ', SymbolicOperation('limit(1/x, x, inf)'));
  79.   Writeln;
  80. end;
  81.  
  82. procedure DemoEquationSolving;
  83. begin
  84.   Writeln('Equation Solving:');
  85.   Writeln('solve(x^2 - 4 = 0, x) = ', SymbolicOperation('solve(x^2 - 4 = 0, x)'));
  86.   Writeln('solve([x + y = 5, x - y = 1], [x, y]) = ',
  87.     SymbolicOperation('solve([x + y = 5, x - y = 1], [x, y])'));
  88.   Writeln;
  89. end;
  90.  
  91. procedure DemoTrigonometry;
  92. begin
  93.   Writeln('Trigonometry:');
  94.   Writeln('sin(pi/2) = ', SymbolicOperation('sin(%pi/2)'));
  95.   Writeln('cos(0) = ', SymbolicOperation('cos(0)'));
  96.   Writeln('trigsimp(sin(x)^2 + cos(x)^2) = ',
  97.     SymbolicOperation('trigsimp(sin(x)^2 + cos(x)^2)'));
  98.   Writeln;
  99. end;
  100.  
  101. begin
  102.  
  103.  
  104.   // Original quadratic equation example
  105.   Writeln('Solving Quadratic Equation:');
  106.   Writeln('x^2 + 5x + 6 = 0');
  107.   Writeln('Result: ', SolveQuadratic(1, 5, 6));
  108.   Writeln;
  109.  
  110.   Writeln('Demonstration of Symbolic Operations in Maxima');
  111.   Writeln('==============================================');
  112.   Writeln;
  113.  
  114.   // Demonstrate various operations
  115.   DemoSimplification;
  116.   DemoDifferentiation;
  117.   DemoIntegration;
  118.   DemoLimits;
  119.   DemoEquationSolving;
  120.   DemoTrigonometry;
  121.  
  122.  
  123.   Writeln('Press Enter to exit...');
  124.   Readln;
  125. end.
  126.  

output:

Code: Text  [Select][+][-]
  1. Solving Quadratic Equation:
  2. x^2 + 5x + 6 = 0
  3. Result: solve(1.0*x^2+5.0*x+6.0 = 0,x)
  4.  
  5. rat: replaced 6.0 by 6/1 = 6.0
  6.  
  7. rat: replaced 5.0 by 5/1 = 5.0
  8.  
  9. rat: replaced 1.0 by 1/1 = 1.0
  10.                               [x = - 3, x = - 2]
  11. quit()
  12.  
  13. Demonstration of Symbolic Operations in Maxima
  14. ==============================================
  15.  
  16. Expression Simplification:
  17. (x + 1)^3 = expand((x+1)^3)
  18.                                3      2
  19.                               x  + 3 x  + 3 x + 1
  20. quit()
  21. (x^2 - 1)/(x - 1) = ratsimp((x^2-1)/(x-1))
  22.                                      x + 1
  23. quit()
  24.  
  25. Differentiation:
  26. d/dx(sin(x)) = diff(sin(x),x)
  27.                                     cos(x)
  28. quit()
  29. d/dx(x^3 + 2*x^2 + x) = diff(x^3+2*x^2+x,x)
  30.                                    2
  31.                                 3 x  + 4 x + 1
  32. quit()
  33. d^2/dx^2(x^3 + 2*x^2 + x) = diff(x^3+2*x^2+x,x,2)
  34.                                     6 x + 4
  35. quit()
  36.  
  37. Integration:
  38. integral of x dx = integrate(x,x)
  39.                                        2
  40.                                       x
  41.                                       --
  42.                                       2
  43. quit()
  44. integral of x^2 + 2*x + 1 dx = integrate(x^2+2*x+1,x)
  45.                                    3
  46.                                   x     2
  47.                                   -- + x  + x
  48.                                   3
  49. quit()
  50. integral of sin(x) dx = integrate(sin(x),x)
  51.                                    - cos(x)
  52. quit()
  53.  
  54. Limits:
  55. lim(x->0) sin(x)/x = limit(sin(x)/x,x,0)
  56.                                        1
  57. quit()
  58. lim(x->inf) 1/x = limit(1/x,x,inf)
  59.                                        0
  60. quit()
  61.  
  62. Equation Solving:
  63. solve(x^2 - 4 = 0, x) = solve(x^2-4 = 0,x)
  64.                                [x = - 2, x = 2]
  65. quit()
  66. solve([x + y = 5, x - y = 1], [x, y]) = solve([x+y = 5,x-y = 1],[x,y])
  67.                                [[x = 3, y = 2]]
  68. quit()
  69.  
  70. Trigonometry:
  71. sin(pi/2) = sin(%pi/2)
  72.                                        1
  73. quit()
  74. cos(0) = cos(0)
  75.                                        1
  76. quit()
  77. trigsimp(sin(x)^2 + cos(x)^2) = trigsimp(sin(x)^2+cos(x)^2)
  78.                                        1
  79. quit()
  80.  
  81. Press Enter to exit...
  82.  

sedsil

  • New Member
  • *
  • Posts: 11
Re: Calling a Maple program in lazarus pascal
« Reply #3 on: October 31, 2025, 12:04:39 pm »

Dear LV,
I have compiled your program and there were no compiler errors. However, it suspends when running, writing out the message "Solving Quadratic Equation:
x² + 5x + 6 = 0". Furthermore, I could not understand how Pascal interacted with Maple. There is a BAT file in the path C:\maxima-5.46.0\bin\maxima.bat. Perhaps the lack of a BAT file is the issue. Could you please explain how to run it?

LV

  • Sr. Member
  • ****
  • Posts: 359
Re: Calling a Maple program in lazarus pascal
« Reply #4 on: October 31, 2025, 02:58:48 pm »

Dear LV,
I have compiled your program and there were no compiler errors. However, it suspends when running, writing out the message "Solving Quadratic Equation:
x² + 5x + 6 = 0". Furthermore, I could not understand how Pascal interacted with Maple. There is a BAT file in the path C:\maxima-5.46.0\bin\maxima.bat. Perhaps the lack of a BAT file is the issue. Could you please explain how to run it?

This Free Pascal program runs the Maxima computer algebra system as an external process, passes it expressions to be calculated, and returns the text results.
This approach also works for Maple:

Code: Pascal  [Select][+][-]
  1. program MapleSolveDemo;
  2.  
  3. uses
  4.   SysUtils, Classes, Process;
  5.  
  6. function RunMapleOnce(const Expr: string): string;
  7. var
  8.   P: TProcess;
  9.   Output: TStringList;
  10.   BytesRead: LongInt;
  11.   Buffer: array[0..4095] of Byte;
  12.   TotalBytes: LongInt;
  13.   ExprWithQuit: string;
  14. begin
  15.   P := TProcess.Create(nil);
  16.   Output := TStringList.Create;
  17.   try
  18.     P.Executable := 'c:\Program Files\Maple 2024\bin.X86_64_WINDOWS\cmaple.exe';
  19.     P.Parameters.Add('-q');
  20.     P.Options := [poUsePipes, poNoConsole];
  21.  
  22.     ExprWithQuit := Expr + '; quit;';
  23.  
  24.     P.Execute;
  25.  
  26.     P.Input.Write(ExprWithQuit[1], Length(ExprWithQuit));
  27.     P.CloseInput;
  28.  
  29.     TotalBytes := 0;
  30.     BytesRead := 0;
  31.     repeat
  32.       BytesRead := P.Output.Read(Buffer, SizeOf(Buffer));
  33.       if BytesRead > 0 then
  34.       begin
  35.         Inc(TotalBytes, BytesRead);
  36.         SetLength(Result, TotalBytes);
  37.         Move(Buffer, Result[TotalBytes - BytesRead + 1], BytesRead);
  38.       end;
  39.     until BytesRead = 0;
  40.  
  41.     P.WaitOnExit;
  42.  
  43.   finally
  44.     Output.Free;
  45.     P.Free;
  46.   end;
  47. end;
  48.  
  49. function SolveQuadratic(a, b, c: Double): string;
  50. var
  51.   cmd: string;
  52.   FmtSettings: TFormatSettings;
  53. begin
  54.   FmtSettings := DefaultFormatSettings;
  55.   FmtSettings.DecimalSeparator := '.';
  56.   cmd := Format('solve(%.2f*x^2 + %.2f*x + %.2f = 0, x);', [a, b, c], FmtSettings);
  57.   Result := RunMapleOnce(cmd);
  58. end;
  59.  
  60. function SymbolicOperation(const Operation: string): string;
  61. begin
  62.   Result := RunMapleOnce(Operation + ';');
  63. end;
  64.  
  65. procedure DemoSimplification;
  66. begin
  67.   Writeln('Expression Simplification:');
  68.   Writeln('(x + 1)^3 = ', #13#10, SymbolicOperation('expand((x + 1)^3)'));
  69.   Writeln('(x^2 - 1)/(x - 1) = ', #13#10, SymbolicOperation('simplify((x^2 - 1)/(x - 1))'));
  70.   Writeln;
  71. end;
  72.  
  73. procedure DemoDifferentiation;
  74. begin
  75.   Writeln('Differentiation:');
  76.   Writeln('d/dx(sin(x)) = ', #13#10, SymbolicOperation('diff(sin(x), x)'));
  77.   Writeln('d/dx(x^3 + 2*x^2 + x) = ', #13#10, SymbolicOperation('diff(x^3 + 2*x^2 + x, x)'));
  78.   Writeln('d2/dx2(x^3 + 2*x^2 + x) = ', #13#10, SymbolicOperation('diff(x^3 + 2*x^2 + x, x, x)'));
  79.   Writeln;
  80. end;
  81.  
  82. procedure DemoIntegration;
  83. begin
  84.   Writeln('Integration:');
  85.   Writeln('integral of x dx = ', #13#10, SymbolicOperation('int(x, x)'));
  86.   Writeln('integral of x^2 + 2*x + 1 dx = ', #13#10, SymbolicOperation('int(x^2 + 2*x + 1, x)'));
  87.   Writeln('integral of sin(x) dx = ', #13#10, SymbolicOperation('int(sin(x), x)'));
  88.   Writeln;
  89. end;
  90.  
  91. procedure DemoLimits;
  92. begin
  93.   Writeln('Limits:');
  94.   Writeln('lim(x->0) sin(x)/x = ', #13#10, SymbolicOperation('limit(sin(x)/x, x=0)'));
  95.   Writeln('lim(x->infinity) 1/x = ', #13#10, SymbolicOperation('limit(1/x, x=infinity)'));
  96.   Writeln;
  97. end;
  98.  
  99. procedure DemoEquationSolving;
  100. begin
  101.   Writeln('Equation Solving:');
  102.   Writeln('solve(x^2 - 4 = 0, x) = ', #13#10, SymbolicOperation('solve(x^2 - 4 = 0, x)'));
  103.   Writeln('solve({x + y = 5, x - y = 1}, {x, y}) = ', #13#10,
  104.     SymbolicOperation('solve({x + y = 5, x - y = 1}, {x, y})'));
  105.   Writeln;
  106. end;
  107.  
  108. procedure DemoTrigonometry;
  109. begin
  110.   Writeln('Trigonometry:');
  111.   Writeln('sin(Pi/2) = ', #13#10, SymbolicOperation('sin(Pi/2)'));
  112.   Writeln('cos(0) = ', #13#10, SymbolicOperation('cos(0)'));
  113.   Writeln('simplify(sin(x)^2 + cos(x)^2) = ', #13#10,
  114.     SymbolicOperation('simplify(sin(x)^2 + cos(x)^2)'));
  115.   Writeln;
  116. end;
  117.  
  118. begin
  119.   Writeln('Solving Quadratic Equation:');
  120.   Writeln('x^2 + 5x + 6 = 0');
  121.   Writeln('Result: ', #13#10, SolveQuadratic(1, 5, 6));
  122.   Writeln;
  123.  
  124.   Writeln('Demonstration of Symbolic Operations in Maple');
  125.   Writeln('=============================================');
  126.   Writeln;
  127.  
  128.   // Demonstrate various operations
  129.   DemoSimplification;
  130.   DemoDifferentiation;
  131.   DemoIntegration;
  132.   DemoLimits;
  133.   DemoEquationSolving;
  134.   DemoTrigonometry;
  135.  
  136.   Writeln('Press Enter to exit...');
  137.   Readln;
  138. end.
  139.  

output:
Code: Text  [Select][+][-]
  1. Solving Quadratic Equation:
  2. x^2 + 5x + 6 = 0
  3. Result:
  4.                                     -2., -3.
  5.  
  6.  
  7.  
  8. Demonstration of Symbolic Operations in Maple
  9. =============================================
  10.  
  11. Expression Simplification:
  12. (x + 1)^3 =
  13.                                3      2
  14.                               x  + 3 x  + 3 x + 1
  15.  
  16.  
  17. (x^2 - 1)/(x - 1) =
  18.                                      x + 1
  19.  
  20.  
  21.  
  22. Differentiation:
  23. d/dx(sin(x)) =
  24.                                      cos(x)
  25.  
  26.  
  27. d/dx(x^3 + 2*x^2 + x) =
  28.                                     2
  29.                                  3 x  + 4 x + 1
  30.  
  31.  
  32. d2/dx2(x^3 + 2*x^2 + x) =
  33.                                     6 x + 4
  34.  
  35.  
  36.  
  37. Integration:
  38. integral of x dx =
  39.                                         2
  40.                                        x
  41.                                       ----
  42.                                        2
  43.  
  44.  
  45. integral of x^2 + 2*x + 1 dx =
  46.                                            3
  47.                                     (x + 1)
  48.                                     --------
  49.                                        3
  50.  
  51.  
  52. integral of sin(x) dx =
  53.                                     -cos(x)
  54.  
  55.  
  56.  
  57. Limits:
  58. lim(x->0) sin(x)/x =
  59.                                        1
  60.  
  61.  
  62. lim(x->infinity) 1/x =
  63.                                        0
  64.  
  65.  
  66.  
  67. Equation Solving:
  68. solve(x^2 - 4 = 0, x) =
  69.                                      2, -2
  70.  
  71.  
  72. solve({x + y = 5, x - y = 1}, {x, y}) =
  73.                                  {x = 3, y = 2}
  74.  
  75.  
  76.  
  77. Trigonometry:
  78. sin(Pi/2) =
  79.                                        1
  80.  
  81.  
  82. cos(0) =
  83.                                        1
  84.  
  85.  
  86. simplify(sin(x)^2 + cos(x)^2) =
  87.                                        1
  88.  
  89.  
  90.  
  91. Press Enter to exit...
  92.  

sedsil

  • New Member
  • *
  • Posts: 11
Re: Calling a Maple program in lazarus pascal
« Reply #5 on: October 31, 2025, 03:26:58 pm »
I am grateful to you for sharing your valuable knowledge. I have one more question. You used the modules implemented in Maple. What happens if I want to run my own Maple module? What changes would there be to your final programme? Thank you.

Dear LV,
I have compiled your program and there were no compiler errors. However, it suspends when running, writing out the message "Solving Quadratic Equation:
x² + 5x + 6 = 0". Furthermore, I could not understand how Pascal interacted with Maple. There is a BAT file in the path C:\maxima-5.46.0\bin\maxima.bat. Perhaps the lack of a BAT file is the issue. Could you please explain how to run it?

This Free Pascal program runs the Maxima computer algebra system as an external process, passes it expressions to be calculated, and returns the text results.
This approach also works for Maple:

Code: Pascal  [Select][+][-]
  1. program MapleSolveDemo;
  2.  
  3. uses
  4.   SysUtils, Classes, Process;
  5.  
  6. function RunMapleOnce(const Expr: string): string;
  7. var
  8.   P: TProcess;
  9.   Output: TStringList;
  10.   BytesRead: LongInt;
  11.   Buffer: array[0..4095] of Byte;
  12.   TotalBytes: LongInt;
  13.   ExprWithQuit: string;
  14. begin
  15.   P := TProcess.Create(nil);
  16.   Output := TStringList.Create;
  17.   try
  18.     P.Executable := 'c:\Program Files\Maple 2024\bin.X86_64_WINDOWS\cmaple.exe';
  19.     P.Parameters.Add('-q');
  20.     P.Options := [poUsePipes, poNoConsole];
  21.  
  22.     ExprWithQuit := Expr + '; quit;';
  23.  
  24.     P.Execute;
  25.  
  26.     P.Input.Write(ExprWithQuit[1], Length(ExprWithQuit));
  27.     P.CloseInput;
  28.  
  29.     TotalBytes := 0;
  30.     BytesRead := 0;
  31.     repeat
  32.       BytesRead := P.Output.Read(Buffer, SizeOf(Buffer));
  33.       if BytesRead > 0 then
  34.       begin
  35.         Inc(TotalBytes, BytesRead);
  36.         SetLength(Result, TotalBytes);
  37.         Move(Buffer, Result[TotalBytes - BytesRead + 1], BytesRead);
  38.       end;
  39.     until BytesRead = 0;
  40.  
  41.     P.WaitOnExit;
  42.  
  43.   finally
  44.     Output.Free;
  45.     P.Free;
  46.   end;
  47. end;
  48.  
  49. function SolveQuadratic(a, b, c: Double): string;
  50. var
  51.   cmd: string;
  52.   FmtSettings: TFormatSettings;
  53. begin
  54.   FmtSettings := DefaultFormatSettings;
  55.   FmtSettings.DecimalSeparator := '.';
  56.   cmd := Format('solve(%.2f*x^2 + %.2f*x + %.2f = 0, x);', [a, b, c], FmtSettings);
  57.   Result := RunMapleOnce(cmd);
  58. end;
  59.  
  60. function SymbolicOperation(const Operation: string): string;
  61. begin
  62.   Result := RunMapleOnce(Operation + ';');
  63. end;
  64.  
  65. procedure DemoSimplification;
  66. begin
  67.   Writeln('Expression Simplification:');
  68.   Writeln('(x + 1)^3 = ', #13#10, SymbolicOperation('expand((x + 1)^3)'));
  69.   Writeln('(x^2 - 1)/(x - 1) = ', #13#10, SymbolicOperation('simplify((x^2 - 1)/(x - 1))'));
  70.   Writeln;
  71. end;
  72.  
  73. procedure DemoDifferentiation;
  74. begin
  75.   Writeln('Differentiation:');
  76.   Writeln('d/dx(sin(x)) = ', #13#10, SymbolicOperation('diff(sin(x), x)'));
  77.   Writeln('d/dx(x^3 + 2*x^2 + x) = ', #13#10, SymbolicOperation('diff(x^3 + 2*x^2 + x, x)'));
  78.   Writeln('d2/dx2(x^3 + 2*x^2 + x) = ', #13#10, SymbolicOperation('diff(x^3 + 2*x^2 + x, x, x)'));
  79.   Writeln;
  80. end;
  81.  
  82. procedure DemoIntegration;
  83. begin
  84.   Writeln('Integration:');
  85.   Writeln('integral of x dx = ', #13#10, SymbolicOperation('int(x, x)'));
  86.   Writeln('integral of x^2 + 2*x + 1 dx = ', #13#10, SymbolicOperation('int(x^2 + 2*x + 1, x)'));
  87.   Writeln('integral of sin(x) dx = ', #13#10, SymbolicOperation('int(sin(x), x)'));
  88.   Writeln;
  89. end;
  90.  
  91. procedure DemoLimits;
  92. begin
  93.   Writeln('Limits:');
  94.   Writeln('lim(x->0) sin(x)/x = ', #13#10, SymbolicOperation('limit(sin(x)/x, x=0)'));
  95.   Writeln('lim(x->infinity) 1/x = ', #13#10, SymbolicOperation('limit(1/x, x=infinity)'));
  96.   Writeln;
  97. end;
  98.  
  99. procedure DemoEquationSolving;
  100. begin
  101.   Writeln('Equation Solving:');
  102.   Writeln('solve(x^2 - 4 = 0, x) = ', #13#10, SymbolicOperation('solve(x^2 - 4 = 0, x)'));
  103.   Writeln('solve({x + y = 5, x - y = 1}, {x, y}) = ', #13#10,
  104.     SymbolicOperation('solve({x + y = 5, x - y = 1}, {x, y})'));
  105.   Writeln;
  106. end;
  107.  
  108. procedure DemoTrigonometry;
  109. begin
  110.   Writeln('Trigonometry:');
  111.   Writeln('sin(Pi/2) = ', #13#10, SymbolicOperation('sin(Pi/2)'));
  112.   Writeln('cos(0) = ', #13#10, SymbolicOperation('cos(0)'));
  113.   Writeln('simplify(sin(x)^2 + cos(x)^2) = ', #13#10,
  114.     SymbolicOperation('simplify(sin(x)^2 + cos(x)^2)'));
  115.   Writeln;
  116. end;
  117.  
  118. begin
  119.   Writeln('Solving Quadratic Equation:');
  120.   Writeln('x^2 + 5x + 6 = 0');
  121.   Writeln('Result: ', #13#10, SolveQuadratic(1, 5, 6));
  122.   Writeln;
  123.  
  124.   Writeln('Demonstration of Symbolic Operations in Maple');
  125.   Writeln('=============================================');
  126.   Writeln;
  127.  
  128.   // Demonstrate various operations
  129.   DemoSimplification;
  130.   DemoDifferentiation;
  131.   DemoIntegration;
  132.   DemoLimits;
  133.   DemoEquationSolving;
  134.   DemoTrigonometry;
  135.  
  136.   Writeln('Press Enter to exit...');
  137.   Readln;
  138. end.
  139.  

output:
Code: Text  [Select][+][-]
  1. Solving Quadratic Equation:
  2. x^2 + 5x + 6 = 0
  3. Result:
  4.                                     -2., -3.
  5.  
  6.  
  7.  
  8. Demonstration of Symbolic Operations in Maple
  9. =============================================
  10.  
  11. Expression Simplification:
  12. (x + 1)^3 =
  13.                                3      2
  14.                               x  + 3 x  + 3 x + 1
  15.  
  16.  
  17. (x^2 - 1)/(x - 1) =
  18.                                      x + 1
  19.  
  20.  
  21.  
  22. Differentiation:
  23. d/dx(sin(x)) =
  24.                                      cos(x)
  25.  
  26.  
  27. d/dx(x^3 + 2*x^2 + x) =
  28.                                     2
  29.                                  3 x  + 4 x + 1
  30.  
  31.  
  32. d2/dx2(x^3 + 2*x^2 + x) =
  33.                                     6 x + 4
  34.  
  35.  
  36.  
  37. Integration:
  38. integral of x dx =
  39.                                         2
  40.                                        x
  41.                                       ----
  42.                                        2
  43.  
  44.  
  45. integral of x^2 + 2*x + 1 dx =
  46.                                            3
  47.                                     (x + 1)
  48.                                     --------
  49.                                        3
  50.  
  51.  
  52. integral of sin(x) dx =
  53.                                     -cos(x)
  54.  
  55.  
  56.  
  57. Limits:
  58. lim(x->0) sin(x)/x =
  59.                                        1
  60.  
  61.  
  62. lim(x->infinity) 1/x =
  63.                                        0
  64.  
  65.  
  66.  
  67. Equation Solving:
  68. solve(x^2 - 4 = 0, x) =
  69.                                      2, -2
  70.  
  71.  
  72. solve({x + y = 5, x - y = 1}, {x, y}) =
  73.                                  {x = 3, y = 2}
  74.  
  75.  
  76.  
  77. Trigonometry:
  78. sin(Pi/2) =
  79.                                        1
  80.  
  81.  
  82. cos(0) =
  83.                                        1
  84.  
  85.  
  86. simplify(sin(x)^2 + cos(x)^2) =
  87.                                        1
  88.  
  89.  
  90.  
  91. Press Enter to exit...
  92.  

LV

  • Sr. Member
  • ****
  • Posts: 359
Re: Calling a Maple program in lazarus pascal
« Reply #6 on: October 31, 2025, 09:08:30 pm »
What happens if I want to run my own Maple module?

I’m not very skilled at Maple, but it's something like that.

Code: Text  [Select][+][-]
  1. module MyMath()
  2.     option package;
  3.     export QuadraticRoots, PowerExpand, Greet;
  4.  
  5.     Greet := proc(name)
  6.         printf("Hello, %s! This is MyMath.\n", name);
  7.     end proc;
  8.  
  9.     QuadraticRoots := proc(a, b, c)
  10.         local d := b^2 - 4*a*c;
  11.         if d < 0 then
  12.             print("No real roots");
  13.         elif d = 0 then
  14.             print([-b/(2*a)]);
  15.         else
  16.             print([(-b + sqrt(d))/(2*a), (-b - sqrt(d))/(2*a)]);
  17.         end if;
  18.     end proc;
  19.  
  20.     PowerExpand := proc(expr, n)
  21.         print(expand(expr^n));
  22.     end proc;
  23.  
  24. end module:
  25.  

Code: Pascal  [Select][+][-]
  1. program MapleCustomModuleDemo;
  2.  
  3. uses
  4.   SysUtils, Classes, Process;
  5.  
  6. const
  7.   MAPLE_PATH   = 'c:\Program Files\Maple 2024\bin.X86_64_WINDOWS\cmaple.exe';
  8.   MODULE_PATH  = 'C:\MapleModules\MyMath.mpl';   // <-- change if you use another folder
  9.  
  10. function RunMaple(const Input: string): string;
  11. var
  12.   P: TProcess;
  13.   Buffer: array[0..4095] of Byte;
  14.   BytesRead, TotalBytes: LongInt;
  15.   Cmd: string;
  16. begin
  17.   if not FileExists(MODULE_PATH) then
  18.     raise Exception.Create('Module file not found: ' + MODULE_PATH);
  19.  
  20.   P := TProcess.Create(nil);
  21.   try
  22.     P.Executable := MAPLE_PATH;
  23.     P.Parameters.Add('-q');
  24.     P.Options := [poUsePipes, poNoConsole];
  25.  
  26.      Cmd := Format('currentdir("%s"): ' +
  27.                   'read "%s": ' +
  28.                   '%s; quit;',
  29.                   [ExtractFilePath(MODULE_PATH),
  30.                    ExtractFileName(MODULE_PATH),
  31.                    Input]);
  32.     Cmd := StringReplace(Cmd, '\', '/', [rfReplaceAll]);
  33.  
  34.     P.Execute;
  35.     P.Input.Write(Cmd[1], Length(Cmd));
  36.     P.CloseInput;
  37.  
  38.     TotalBytes := 0;
  39.     repeat
  40.       BytesRead := P.Output.Read(Buffer, SizeOf(Buffer));
  41.       if BytesRead > 0 then
  42.       begin
  43.         Inc(TotalBytes, BytesRead);
  44.         SetLength(Result, TotalBytes);
  45.         Move(Buffer, Result[TotalBytes - BytesRead + 1], BytesRead);
  46.       end;
  47.     until BytesRead = 0;
  48.  
  49.     P.WaitOnExit;
  50.   finally
  51.     P.Free;
  52.   end;
  53. end;
  54.  
  55. procedure Demo;
  56. begin
  57.   Writeln('=== Custom Maple module MyMath ===');
  58.   Writeln;
  59.  
  60.   Writeln(RunMaple('MyMath:-Greet("@sedsil")'));
  61.   Writeln;
  62.  
  63.   Writeln('QuadraticRoots(1,5,6):');
  64.   Writeln(RunMaple('MyMath:-QuadraticRoots(1,5,6)'));
  65.   Writeln;
  66.  
  67.   Writeln('PowerExpand(x+2, 4):');
  68.   Writeln(RunMaple('MyMath:-PowerExpand(x+2, 4)'));
  69.   Writeln;
  70. end;
  71.  
  72. begin
  73.   try
  74.     Demo;
  75.     Writeln('Done. Press Enter to exit...');
  76.     Readln;
  77.   except
  78.     on E: Exception do
  79.       Writeln('ERROR: ', E.Message);
  80.   end;
  81. end.
  82.  

output:
Code: Text  [Select][+][-]
  1. === Custom Maple module MyMath ===
  2.  
  3. Hello, @sedsil! This is MyMath.
  4.  
  5.  
  6. QuadraticRoots(1,5,6):
  7.                                     [-2, -3]
  8.  
  9.  
  10.  
  11. PowerExpand(x+2, 4):
  12.                           4      3       2
  13.                          x  + 8 x  + 24 x  + 32 x + 16
  14.  
  15.  
  16.  
  17. Done. Press Enter to exit...
  18.  

 

TinyPortal © 2005-2018