当前位置:网站首页>WSTP初体验
WSTP初体验
2022-08-10 05:29:00 【溴锑锑跃迁】
本文主要基于:
环境:
- Qt 5.14.1(Qt Creator 4.11.0 Community)
- Mathematica 10.3(不要嫌弃是老版本,主要是没经费买个正版的)
- Windows 10
我们的目标是实现C++与Mathematica的交互使用,本文主要介绍如何在C++里使用Mathematica提供的强大符号计算系统
0.配置:
梗概操作可以看上面那篇知乎的文章。细节上,注意几个点:
- 动态链接库(dll)没问题,静态库(lib)注意是wstp32i4m.lib或者wstp64i4m.lib,末尾有一个小写字母m
- 知乎写主说到了一个问题,就是“无法解析的外部符号”以及两个动态链接库真正目的位置是反的,这里建议把32位和64位的库一起都复制到两个系统文件夹
- 用Qt工程的添加库功能时,选择外部库,把Linux和Mac平台去掉,debug模式不在末尾加小写字母d,此外,双击.pro文件进行编辑,把关于wstp32i4m.a的编译选项全部删掉
- 不是说你写完代码运行就完事了,一定要运行Mathematica,注意你在Mathematica里写的代码有个Listen(监听),玩服务器的可能熟
1.梗概分析:
示例代码可以去看那个大佬在知乎里写的文章,其中代码有这样几个问题:1.没有缩进!!!2.main函数被定义为void,实际上标准是int(这里吐槽微软一发,Visual Studio允许你在保证没毛病的情况下写下void main() )。我这里仅是做一个总结。
首先我们注意到了这样几个固定的代码(块):
char *argv[] = { ... };
Link = WSOpen(argc, argv);
if ((WSLINK)0 == Link)
cout << " Unable to create the link..." << endl;
else
cout << "Link is Created successfully..." << endl;(来自类构造方法)
对第一行代码,官方给出明确的解释:

注意在名称、协议、模式上的一一对应
后面紧接着的if语句实际上就是异常处理,概念上不可或缺,实际操作上见仁见智,下面这段代码也是:
WSENV env = WSInitialize((WSEnvironmentParameter)0);
if ((WSENV)0==env)
cout << "Unable to initialize environment..." << endl;
else
cout << "The environment is initialized successfully..." << endl;(来自main函数)
我们还注意到,那位知乎写主都是用了类来封装程序操作的,实际上Wolfram并不对此做硬性要求,这样写可读性很强、也方便理解和添加新功能。
2.实现浅谈:
这部分再精炼、再完善也不会超过官方文档(详见引用2),希望你能在阅读之前大致浏览一下API分类:

大致流程:

3.跋
1.可能出现的错误/警告:
warning: ISO C++11 does not allow conversion from string literal to 'char *'
可以直接忽略,无伤大雅,有待解决(2020年7月21日)
无法解析的外部符号 / undefined reference
如果后面的信息里出现了WSTP API的名称,可以基本确定是动态库配置不正确,解决方法已在上文提到了
2.彩蛋:
稍微抽象提升了一下:
#include"wstp.h"
#include<iostream>
#include<cmath>
#include<stdlib.h>
#include<string>
using namespace std;
class Expression
{
private:
WSLINK Link;
string exp;
const char *String;
public:
Expression(string str) : exp(str)
{
int argc = 4;
char *argv[5] = { "-linkname", "Expression", "-linkmode", "connect", NULL };
Link = WSOpen(argc, argv);
if ((WSLINK)0 == Link)
cout << " Unable to create the link..." << endl;
else
cout << "Link is Created successfully..." << endl;
}
~Expression(){}
void ExpToMathematica()
{
//向Mathematica发送表达式,使其计算符号积分
WSPutFunction(Link, "EvaluatePacket", 1);
WSPutFunction(Link, "ToExpression", 1);
WSPutString(Link, exp.c_str());
WSEndPacket(Link);
}
string SymbExpToC()
{
//得到Mathematica计算后的符号积分结果,并返回
WSNewPacket(Link);
WSGetString(Link, &String);
string res(String);
WSReleaseString(Link, String);
WSEndPacket(Link);
return res;
}
void LinkClose()
{
WSClose(Link);
cout << "The link has been shuted down..." << endl;
}
};
int main()
{
WSENV env = WSInitialize((WSEnvironmentParameter)0);
if ((WSENV)0==env)
cout << "Unable to initialize environment..." << endl;
else
cout << "The environment is initialized successfully..." << endl;
Expression exp("f[x_]=Integrate[Sin[x],x]");
exp.ExpToMathematica();
exp.SymbExpToC();
exp.LinkClose();
WSDeinitialize(env);
system("pause");
return 0;
}
对于普通的符号计算已经足矣(数值计算还有一个坑,过后填上=.=),一定要搭配上相适应的Wolfram代码:
Link=LinkCreate["Expression",LinkProtocol->"SharedMemory",LinkMode->Listen];
SymbolForm=LinkRead[Link];
LinkWrite[Link,ToString[f[x]//CForm]];
LinkWrite[Link,f[x]]
LinkClose[Link]具体遇到的bug也过后再说吧!
边栏推荐
- Advanced Feature Selection Techniques in Linear Models - Based on R
- SQL Server query optimization
- aliases节点分析
- Talk about API Management - Open Source Edition to SaaS Edition
- Important transformation and upgrading
- An article to master the entire JVM, JVM ultra-detailed analysis!!!
- CORS跨域资源共享漏洞的原理与挖掘方法
- 软考考生注意!2022年下半年报名详细流程来了!
- 看了几十篇轻量化目标检测论文扫盲做的摘抄笔记
- Jenkins 如何玩转接口自动化测试?
猜你喜欢

一文带你搞懂OAuth2.0

大咖说·对话生态|当Confluent遇见云:实时流动的数据更有价值

手把手带你写嵌入式物联网的第一个项目

MySQL simple tutorial

EasyGBS connects to mysql database and prompts "can't connect to mysql server", how to solve it?

Kubernetes:(十七)Helm概述、安装及配置

Practical skills 19: Several postures of List to Map List

Qiskit学习笔记(三)

Rpc interface stress test

如何从代码层提高产品质量
随机推荐
OAuth2 usage scenarios, common misunderstandings, use cases
SEO搜索引擎优化
The time for flinkcdc to read pgsql is enlarged. Does anyone know what happened? gmt_create':1
Read the excerpt notes made by dozens of lightweight target detection papers for literacy
Nexus_Warehouse Type
重要转型升级
如何从代码层提高产品质量
Important transformation and upgrading
论文精读 —— 2021 CVPR《Progressive Temporal Feature Alignment Network for Video Inpainting》
一文带你搞懂OAuth2.0
手把手带你写嵌入式物联网的第一个项目
Depth of carding: prevent model fitting method
mysql cdc (2.1.1)inital snapshot数据库的时候设置了5个并发度,se
软考考生注意!2022年下半年报名详细流程来了!
`id` bigint(20) unsigned NOT NULL COMMENT 'Database primary key',
请教一下各位大佬。CDC社区中FlinkCDC2.2.0版本有说明支持的sqlserver版本 ,请
Conda creates a virtual environment method and pqi uses a domestic mirror source to install a third-party library method tutorial
I have a dream for Career .
Flutter development: error The following assertion was thrown resolving an image codec: Solution for Unable to...
FPGA工程师面试试题集锦1~10