如何挂钩MATLAB以评估C代码中的符号衍生物?
答
下面是一个工作示例,其中包含C
以及用于gcc \ Linux的Makefile
。如果任何人都可以提供Windows或Mac的构建说明,请执行。
首先C
代码:
#define _GNU_SOURCE
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <engine.h>
int main (int argc, char *argv[])
{
Engine *engine;
mxArray *f;
mxChar *output;
char *input;
char *command;
size_t input_size;
int i;
engine = engOpen ("matlab -nojvm");
engEvalString (engine, "syms x");
input = NULL;
printf (">> "); getline (&input, &input_size, stdin);
while (!feof (stdin)) {
// remove the \n character
input[strlen (input) - 1] = '\0';
command = (char *)malloc (strlen (input) + 19);
sprintf (command, "f=simple(diff(%s,x))", input);
// execute the `diff` command on the user input expression
engEvalString (engine, command);
// the MATLAB engine does not understand the Symbolic Toolbox
// therefore you have to convert the output expression into
// some textual form (ccode,fortran,matlabFunction,latex)
engEvalString (engine, "fstr=ccode(f)");
f = engGetVariable (engine, "fstr");
output = mxGetChars (f);
// the expression is prefixed with some left-hand line (eg, "t0=")
// so, discard everything to the left of the =, and display just
// the derivative
for (i = 0; output[i] != '='; i++);
i++; // skip the =
// `mxChar` is a 16-bit character, and `printf ("%ls")` is giving
// me errors, so go through the string one character at a time, until
// the semicolon is found (it is C-code), ignoring non-printable
// characters along the way.
for (; output[i] != ';'; i++) {
if (isgraph (output[i])) {
putchar (output[i]);
}
}
putchar ('\n');
printf (">> "); getline (&input, &input_size, stdin);
}
printf ("exiting...\n");
engClose (engine);
return EXIT_SUCCESS;
}
如果你需要使用的表达,而不是仅仅呼应它,你将不得不写自己的解析器。但是由于它是一个简单的C表达式(使用math.h
函数),它是相当平凡的。
这里是我的Makefile,没有什么奇特的..只记得适当地设置你的路径。
CC=gcc
CFLAGS=-O0 -g -Wall -I$(MATLAB_DIR)/extern/include
LDFLAGS=-L$(MATLAB_DIR)/bin/glnxa64 -leng -lmx -lmat -lut -licudata -licuuc -licui18n -licuio -lhdf5
MATLAB_DIR=/opt/matlab/2008a
%.o: %.c
$(CC) $(CFLAGS) -c $< -o [email protected]
diff_test: diff_test.o
$(CC) $< -o [email protected] $(LDFLAGS)
最后一个快速演示:
# ./diff_test
>> sin(x)
cos(x)
>> 2*x^2*tan(x)
2.0*x*(2.0*tan(x)+x+x*pow(tan(x),2.0))
>> log(x^2)
2.0/x
>> ^D
exiting...
这是个问题吗? – second 2009-06-12 13:35:57