My Project
machinelearning.cc
Go to the documentation of this file.
1 #include <stdio.h>
2 
3 #include "kernel/mod2.h"
4 #if defined(HAVE_READLINE) && defined(HAVE_READLINE_READLINE_H)
5 #include <readline/history.h>
6 #include "Singular/ipid.h"
7 #include "Singular/mod_lib.h"
9 
10 #ifdef HAVE_PYTHON
11 
12 #include "mlpredict.h"
13 
14 static BOOLEAN predictHelp(leftv result, leftv arg)
15 {
16  char *buffer[5];
17  int lengths[5];
18  char *filename = getenv("SINGULARHIST");
19  if (filename==NULL) filename=SINGULARHIST_FILE;
20  int i;
22 
23  buffer[0] = NULL;
24  buffer[1] = NULL;
25  buffer[2] = NULL;
26  buffer[3] = NULL;
27  buffer[4] = NULL;
28 
29  if (write_history(filename))
30  {
31  printf("Failed to write history\n");
32  return TRUE;
33  }
34 
35  if (!ml_initialise())
36  {
37  WerrorS("Initialise of ml failed.");
38  /* Notify singular that an error occured */
39  return TRUE;
40  }
41  #ifdef HAVE_OMALLOC
42  ml_make_prediction(filename, buffer, lengths, _omStrDup);
43  #else /*xalloc*/
44  ml_make_prediction(filename, buffer, lengths, strdup);
45  #endif
46 
47  L->Init(5);
48 
49  for (i = 0; i < 5; i++) {
50  //printf("prediction %d: %s\n", i, buffer[i]);
51  L->m[i].rtyp = STRING_CMD;
52  L->m[i].data = buffer[i];
53  }
54 
55  // pass the resultant list to the res datastructure
56  result->rtyp=LIST_CMD;
57  result->data=(void *)L;
58 
59  ml_finalise();
60  return FALSE;
61 
62  //result->rtyp=NONE; // set the result type
63 }
64 
65 // initialisation of the module
66 extern "C" int SI_MOD_INIT(machinelearning)(SModulFunctions* psModulFunctions)
67 {
68  char *filename = getenv("SINGULARHIST");
69  if (filename==NULL)
70  {
71  WarnS("SINGULARHIST is not set - no history available, using ~/.singularhist");
72  }
73  // this is the initialization routine of the module
74  // adding the routine predictHelp:
75  psModulFunctions->iiAddCproc(
76  (currPack->libname? currPack->libname: ""),
77  "predictHelp",// for the singular interpreter
78  FALSE, // should enter the global name space
79  predictHelp); // the C/C++ routine
80  return MAX_TOK;
81 }
82 #endif
83 #endif
int BOOLEAN
Definition: auxiliary.h:87
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
int i
Definition: cfEzgcd.cc:132
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
int rtyp
Definition: subexpr.h:91
void * data
Definition: subexpr.h:88
Definition: lists.h:24
sleftv * m
Definition: lists.h:46
INLINE_THIS void Init(int l=0)
#define WarnS
Definition: emacs.cc:78
return result
Definition: facAbsBiFact.cc:75
void WerrorS(const char *s)
Definition: feFopen.cc:24
char * getenv()
int write_history()
#define SINGULARHIST_FILE
Definition: feread.h:20
VAR package currPack
Definition: ipid.cc:57
VAR omBin slists_bin
Definition: lists.cc:23
Function definitions for using python to do machine learning in Singular.
slists * lists
Definition: mpr_numeric.h:146
#define omAllocBin(bin)
Definition: omAllocDecl.h:205
#define strdup
Definition: omAllocFunc.c:18
OM_INLINE_DECL char * _omStrDup(const char *s)
#define NULL
Definition: omList.c:12
@ LIST_CMD
Definition: tok.h:118
@ STRING_CMD
Definition: tok.h:185
@ MAX_TOK
Definition: tok.h:218