python调用mathematica代码_Mathematica与python的socket通信

由于有时需要将复杂的符号计算的结果传递给python用于时间的仿真控制。通过采用socket的通信能够实现不同程序之间的通信。

这里设置host,IP为127.0.0.1,端口为65435。端口应该大于1023。

Mathematica脚本如下:

Clear["`*"]

socket=SocketConnect[{"127.0.0.1",65435}];

(*设置传输数据*)

m = 2.93232;

x = ToString[m];

Y = x <>" "<> x;

WriteString[socket,Y];(*写入数据*)

message=SocketReadMessage[socket];(*读取数据*)

str = ByteArrayToString[message];

num = StringSplit[str];(*分割字符串*)

Print[ToExpression[num[[1]]]];

python服务端server.py脚本代码如下:

#!/usr/bin/env python3

import socket

HOST = '127.0.0.1' # Standard loopback interface address (localhost)

PORT = 65435 # Port to listen on (non-privileged ports are > 1023)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:

s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

s.bind((HOST, PORT))

s.listen()

conn, addr = s.accept()

with conn:

print('Connected by', addr)

while True:

data = conn.recv(1024)

if not data:

break

datalist = data.split()

lenx = len(datalist)

print('from mm:')

for i in range(lenx):

print(float(datalist[i]))

strx = '12.0 13.0'

conn.sendall(strx.encode())

s.close()

可以实现简单mathematica与python之间的浮点数组的传输与解析。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mathematica 是数学领域的一个巨人,发布的 Mathematica 软件在各个前沿行业的科学计算,仿真,建模等应用中发挥着关键作用。 我下载了45个典型的 Mathematica 演示范例,供打包下载学习。 AreTheseLinesStraight-source.nb BoilingPointOfWaterOnSeveralPlanets-source.nb CassiniSpaceflight-source.nb CellularAutomataOnTrivalentNetworks-source.nb CIEChromaticityDiagram-source.nb CogwheelDrive-source.nb ComplexAdditionOfHarmonicMotionsAndThePhenomenonOfBeats-source. CosineOffsetCurves-source.nb CurlicueFractal-source.nb DayAndNightWorldClock-source.nb DigitalTiltShiftPhotography-source.nb Emoticon-source.nb IcosahedronBall-source.nb InsulinMolecule-source.nb JackLewSignatureFunction-source.nb KeyboardAndComposer-source.nb KomanVariations-source.nb LengthScalesInTheSolarSystem-source.nb LeonardoDaVincisOrnithopter-source.nb LookingAtACube-source.nb MicrowaveOven-source.nb MosaicEffectForPhotographicImages-source (1).nb MosaicEffectForPhotographicImages-source.nb Mountainscape-source.nb PicturePuzzle-source.nb PicturePuzzle.cdf PolynomialAndDerivative-source.nb RadialEngine-source.nb ReliefShadedElevationMap-source.nb SaturnsSeasonalSundial-source.nb SegmentingAMedicalImage-source.nb SmileyChanger-source.nb SpringReturnButton-source.nb SunflowerSeedArrangements-source.nb SunflowerSeedArrangements-source.pdf SurfaceWithBranchCuts-source.nb TermWeightingWithTFIDF-source.nb TheUlamPrimeSpiral-source.nb ThickeningAPolygonMeshForRapidPrototyping3DPrinting-source.nb TravelingSalesmanArt-source.nb TreeBender-source.nb Tries-source.nb TwentyDodecahedronTowers-source.nb VoronoiImage-source.nb WhirlingPolygons-source.nb

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值