C++中调用SPLUS对象经典例子

本文提供了一个S-PLUS编程示例,展示了如何创建到S-PLUS的连接、在S数据库中创建持久对象以及评估S表达式。示例通过创建一系列数学对象并进行回归分析来演示这些步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此例子是我再SPLUS的自带的例子上修改而来,对初学者具有较高的阅读价值

// spllm.cxx : a simple console application that illustrates how to
// 1. create a connection to S-PLUS.
// 2. create permanent objects in S database.
// 3. evaluate an S expression.
//
// Steps to build and test this program from a DOS prompt.
// 1. Change current directory to directory containing this file
//     cd <directory containing this file>
// 2. Build the program
//     msdev spllm.dsp /make
// 3. Run the program
//     spllm.exe S_HOME=%SHOME%
// 4. Run the S-PLUS console
//     %SHOME%/cmd/sqpe.exe
// 5. Look for S objects: x, y and z.  For example,
//     > objects()

#include "sconnect.h"
#include "iostream"
#include "string"
using namespace std;
//A global connection object
CSPengineConnect g_engineConnect;

int main(int argc, char* argv[])
{
 //Create the connection to S-PLUS
 g_engineConnect.Create( argc, argv);
 //Create S object with name "x" in the current database.
 //Same as x<-1:10 at the command line.
 string x;
 cout<<"输入数目:";
 cin>>x;
 x="1:"+x;
 CSPnumeric sx;
 sx.Create(x.c_str(),"x");
 //Squaring sx, which is the same as S expression sy <- x*x in a local frame,
 //but here we set it to local C++ variable sy.
 CSPnumeric sy = sx * sx;
 int i ;
 for( i=0;i<sy.length();i++)
 cout<<sy[i]<<endl;
 // Assign the result as S object with name "y" in the current database.
 sy.Assign("y");
 //Evaluate z<-lm(y~x)
 g_engineConnect.SyncParseEval("z<-lm(y~x)");
 CSPlist result = g_engineConnect.get("z");
 CSPnumeric rank=result.GetAt("rank");
 cout<<"rank:";
 for( i=0;i<rank.length();i++)
  cout<<rank[i];
 cout<<endl;
 CSPcharacter call=result.GetAt("call");
 cout<<"回归表达式:" <<(char *)call(1) <<"=";
 for( i=1;i<call.length();i++)
  cout<<(char *)call[i];
 cout<<endl;
 CSPmatrix coefficients =result.GetAt("coefficients");
 cout<<"回归截距:"<<(double)coefficients(1,1)<<endl;
 cout<<"回归系数:"<<(double)coefficients(2,1)<<endl;
 return 1;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值