1

In the following program calculates the determinant in c via Octave. But i don´t know how to pass the variable from octave to c:

#include <stdio.h>


int main(void)
{ 
    FILE *graf;
    graf=popen("octave -persist","w");
    if (graf==NULL){
        printf("Error \n");}
    else
    { 
        int a;
 printf("input the dimension: \n");
 scanf("%d",&a);
 fprintf(graf,"N= %d;",a);
 fprintf(graf,"pkg load statistics \n");
 fprintf(graf,"M=N-1;");
 fprintf(graf," for i=1:%d \n",a);
 fprintf(graf," for j=1:%d \n",a);
 fprintf(graf," matriz(i,j)=round(1+9*rand());");
 fprintf(graf," endfor \n");
 fprintf(graf,"endfor \n");
 fprintf(graf,"C = combnk(1:%d,M);",a);
 fprintf(graf,"Combinaciones=size(C,1);");
 fprintf(graf,"for i=1:Combinaciones \n");
 fprintf(graf,"for j=1:Combinaciones \n");
 fprintf(graf,"for h=1:M \n");
 fprintf(graf,"for k=1:M \n");
 fprintf(graf,"matrices(h,k)=matriz(C(i,h),C(j,k));");
 fprintf(graf,"endfor \n");
 fprintf(graf,"endfor \n");
 fprintf(graf,"nuevamatriz(Combinaciones-i+1,Combinaciones-j+1)=det(matrices);");
 fprintf(graf,"endfor \n");
 fprintf(graf,"endfor \n");
 fprintf(graf,"matriz \n");
 fprintf(graf,"det(matriz) \n");
 fprintf(graf,"nuevamatriz \n");
 fprintf(graf,"det(nuevamatriz) \n");
pclose(graf);
    }
printf("Have that the second determinant (attached matrix) is always the first (matrix) powered at dimension less one \n\n ");
}

A way to define this determining variable in c.

2 Answers2

0

First: I use -persist because it's the same via to call gnuplot. Second: It motivated me to use this method was It calculating the determinant of any matrix without do so much Code. But I have solved It embedding Python in c with the header < Python.h> and compiling It like this: gcc -I/usr/include/python3.11/ program.c -lpython3.11. -o program since Python include the module numpy and It's more easy even than with octave. Thank you and here, an example:

`

#include <Python.h>
     int main() {
char a[27]="[[1,2,3],[90,23,3],[1,5,7]]";
Py_Initialize();
PyRun_SimpleString("import numpy as np");
PyRun_SimpleString("det=np.linalg.det([[1,2,3],[90,23,3],
[1,5,7]])");
PyRun_SimpleString("print('the determinant is:',det)");
Py_Finalize();
return 0;
}`
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 16 '23 at 15:14
  • You can do exactly the same thing with Octave, see linked duplicate or the [documentation](https://docs.octave.org/v8.2.0/Standalone-Programs.html#Standalone-Programs). – Cris Luengo May 24 '23 at 14:15
0

it can also be done as follows,so the variable is stored in a file and then it can be recovered. This program reads an square matrix like a list of numbers starting with a number that is the dimension from a file, after save the determinant in another file. I use it to compare matrices and determinants.

#include <stdio.h>
void enteroACadena(int numero, char *bufer);
int main(void)
{
char file[10];
printf("Enter the name of the file: \n");
scanf("%s",file);
FILE *fp;
 FILE *graf;
 graf=popen("octave","w");
 fprintf(graf,"clear; \n");
int buffer[50][50];
int g;
fp = fopen ( file, "r" );
fscanf(fp, "%d", &g);
for(int r1=1;r1<g+1;r1++)
{
for(int r2=1;r2<g+1;r2++)
{
fscanf(fp, "%d" ,&buffer[r1][r2]);
char cadena[11]; // 
    char cadena1[3];
    char cadena2[3];
    enteroACadena(buffer[r1][r2], cadena);
    enteroACadena(r1, cadena1);
    enteroACadena(r2, cadena2);
fprintf(graf,"matriz(%s,%s)=%s;\n",cadena1,cadena2,cadena);
//printf("matriz(%s,%s)=%s;  ",cadena1,cadena2,cadena);
}
    printf("\n");
    }

 fprintf(graf,"d=det(matriz) \n");
 fprintf(graf,"save %s.det d \n",file);
pclose(graf);

fclose ( fp );

return 0;
}
void enteroACadena(int numero, char *bufer){

    sprintf(bufer, "%d", numero);
}