Browse Source

First draft : pointset class, and link with dem_discr

Olivier Marty 8 years ago
parent
commit
91fb6aeaf6
8 changed files with 111 additions and 1 deletions
  1. 2 0
      .gitignore
  2. 16 0
      src/Makefile
  3. 0 1
      src/discr_calc/.gitignore
  4. 10 0
      src/main.cpp
  5. 22 0
      src/pointset.cpp
  6. 14 0
      src/pointset.h
  7. 31 0
      src/utils.cpp
  8. 16 0
      src/utils.h

+ 2 - 0
.gitignore

@@ -23,3 +23,5 @@
 *.synctex.gz
 _build/
 main.pdf
+*.o
+/src/main

+ 16 - 0
src/Makefile

@@ -0,0 +1,16 @@
+CPP=g++ -Wall -O2
+CC=gcc
+
+main: discr_calc/dem_discr.o discr_calc/bz_discr.o utils.o pointset.o main.o utils.h
+	$(CPP) $^ -o $@
+
+%.o: %.cpp
+	$(CPP) -c $< -o $@
+
+%.o: %.c
+	$(CC) -c $< -o $@
+
+.PHONY: clean
+
+clean:
+	rm -f *.o discr_calc/*.o main

+ 0 - 1
src/discr_calc/.gitignore

@@ -1 +0,0 @@
-*_discr

+ 10 - 0
src/main.cpp

@@ -0,0 +1,10 @@
+#include <cstdio>
+#include <cstdlib>
+#include "pointset.h"
+
+int main(int argc, char **argv) {
+  int npoints = 3, dim = 2;
+  printf("%g\n", pointset(dim, npoints).discrepancy());
+
+  return EXIT_SUCCESS;
+}

+ 22 - 0
src/pointset.cpp

@@ -0,0 +1,22 @@
+#include "pointset.h"
+#include "utils.h"
+
+extern "C" {
+  #include "discr_calc/dem_discr.h"
+}
+
+pointset::pointset(int dim, int npoints) : dim(dim), npoints(npoints) {
+  data = (double**)malloc2d(npoints, dim, sizeof(double));
+  for(int y = 0; y < npoints; y++)
+    for(int x = 0; x < dim; x++)
+       data[y][x] = 0.5;
+}
+
+pointset::~pointset() {
+  free2d((void**)data, npoints);
+}
+
+double pointset::discrepancy() {
+  double lower;
+  return oydiscr(data, dim, npoints, &lower);
+}

+ 14 - 0
src/pointset.h

@@ -0,0 +1,14 @@
+#ifndef POINTSET_H
+#define POINTSET_H
+
+class pointset {
+  public:
+    pointset(int dim, int npoints);
+    ~pointset();
+    double discrepancy();
+  protected:
+    int dim, npoints;
+    double **data;
+};
+
+#endif // POINTSET_H

+ 31 - 0
src/utils.cpp

@@ -0,0 +1,31 @@
+#include "utils.h"
+#include <cstdio>
+
+/*
+ * malloc a 2d array of size y*x
+ */
+void **malloc2d(int y, int x, size_t size) {
+  void** p = NULL;
+  p = (void**)malloc(sizeof(void*[y]));
+  if(p == NULL) {
+    perror("malloc()");
+    exit(EXIT_FAILURE);
+  }
+  for(int i = 0; i < y; i++) {
+    p[i] = malloc(x*size);
+    if(p[i] == NULL) {
+      perror("malloc()");
+      exit(EXIT_FAILURE);
+    }
+  }
+  return p;
+}
+
+/*
+ * free a 2d array of size y*_
+ */
+void free2d(void** p, int y) {
+  for(int i = 0; i < y; i++)
+    free(p[i]);
+  free(p);
+}

+ 16 - 0
src/utils.h

@@ -0,0 +1,16 @@
+#ifndef UTILS_H
+#define UTILS_H
+
+#include <cstdlib>
+
+/*
+ * malloc a 2d array of size y*x
+ */
+void **malloc2d(int y, int x, size_t size);
+
+/*
+ * free a 2d array of size y*_
+ */
+void free2d(void** p, int y);
+
+#endif // UTILS_H