Skip to content

Commit

Permalink
Added example client code for float and long double precisions.
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencwilliams committed Jan 20, 2012
1 parent 9727cda commit 9f98936
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
12 changes: 10 additions & 2 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
.PHONY: clean
.PHONY: all clean

all: example_float example example_long_double

example_float: example_float.cpp
$(CXX) $(CXXFLAGS) -lbct_float -lgsl -lgslcblas -o $@ $<

example: example.cpp
$(CXX) $(CXXFLAGS) -lbct -lgsl -lgslcblas -o $@ $<

example_long_double: example_long_double.cpp
$(CXX) $(CXXFLAGS) -lbct_long_double -lgsl -lgslcblas -o $@ $<

clean:
-rm example
-rm example_float example example_long_double
38 changes: 38 additions & 0 deletions example/example_float.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <bct/bct_float.h>
#include <cstdio>
#include <gsl/gsl_matrix_float.h>
#include <gsl/gsl_vector_float.h>

int main() {

// Create an uninitialized 30-node square matrix
gsl_matrix_float* m = gsl_matrix_float_alloc(30, 30);

// Initialize the matrix with the data in example.dat
FILE* f = std::fopen("example.dat", "r");
gsl_matrix_float_fscanf(f, m);
std::fclose(f);

// Display the matrix
bct_float::printf(m, "%g");

// Declare variables for optional returns
gsl_vector_float* id; // In-degree
gsl_vector_float* od; // Out-degree

// Calculate degree distribution
gsl_vector_float* deg = bct_float::degrees_dir(m, &id, &od);

// Display the results
bct_float::printf(id, "%g");
bct_float::printf(od, "%g");
bct_float::printf(deg, "%g");

// Free all memory
gsl_matrix_float_free(m); // Could use bct::gsl_free(m)
gsl_vector_float_free(id); // Could use bct::gsl_free(id)
gsl_vector_float_free(od); // ...
gsl_vector_float_free(deg);

return 0;
}
38 changes: 38 additions & 0 deletions example/example_long_double.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <bct/bct_long_double.h>
#include <cstdio>
#include <gsl/gsl_matrix_long_double.h>
#include <gsl/gsl_vector_long_double.h>

int main() {

// Create an uninitialized 30-node square matrix
gsl_matrix_long_double* m = gsl_matrix_long_double_alloc(30, 30);

// Initialize the matrix with the data in example.dat
FILE* f = std::fopen("example.dat", "r");
gsl_matrix_long_double_fscanf(f, m);
std::fclose(f);

// Display the matrix
bct_long_double::printf(m, "%Lg");

// Declare variables for optional returns
gsl_vector_long_double* id; // In-degree
gsl_vector_long_double* od; // Out-degree

// Calculate degree distribution
gsl_vector_long_double* deg = bct_long_double::degrees_dir(m, &id, &od);

// Display the results
bct_long_double::printf(id, "%Lg");
bct_long_double::printf(od, "%Lg");
bct_long_double::printf(deg, "%Lg");

// Free all memory
gsl_matrix_long_double_free(m); // Could use bct::gsl_free(m)
gsl_vector_long_double_free(id); // Could use bct::gsl_free(id)
gsl_vector_long_double_free(od); // ...
gsl_vector_long_double_free(deg);

return 0;
}

0 comments on commit 9f98936

Please sign in to comment.