I have set of data point (x_i,y_i) from a text file. How can I write a C-program that reads those data, send the data to Sage, computes the Pearson correlation and send the result back to C. I have no idea how can I use C to give input to some Linux-program and read its output to a variable.
Asked
Active
Viewed 151 times
-2
-
You are talking about [this Sage](http://sagemath.org/) right? – huon Oct 12 '12 at 07:59
-
Yes. Mathematics software system. – Jaakko Seppälä Oct 12 '12 at 08:02
-
Can't you just do the whole thing in Sage? (`open("filename").read()` etc.) – huon Oct 12 '12 at 08:13
-
I'm not sure about that. Probably but I haven't used Sage that much. – Jaakko Seppälä Oct 12 '12 at 08:15
-
Have you used Python at all? It's the same language. Also, Sage would be much easier than C. Assuming the file is `(x_1,y_1)\n(x_2,y_2)\n...` (where `\n` is a newline), then `list = [map(float, line.strip().split(',')) for line in open("filename")]` gives you a list of `[x_i,y_i]` pairs. – huon Oct 12 '12 at 08:20
1 Answers
0
OK, let me get it straight: you are working on a C program, and within that program you need to calculate Pearson's correlation coefficient. You'd like to pass these calculations to Sage rather than code them yourself.
Now, I don't know Sage, but I guess it is possible to run it from command line. Assuming that you can prepare an input file or files for Sage, and run the calculations in Sage producing an output, I would then use the system
from stdlib.h (man 3 system
) to call the command line. Here is the outline of the steps in your C program:
- prepare temporary files for Sage's input and output
- construct the command line of Sage
- use system() to run the command line
- parse the temporary file name where Sage stored it's output.
That said, I would not do it using Sage. Pearson correlation coefficient is easy enough to implement in C, and if you do it, your program will not depend on the whole Sage installation.

January
- 16,320
- 6
- 52
- 74
-
Well, I just like to learn different methods to solve problems. – Jaakko Seppälä Oct 12 '12 at 08:09