From 9f98936f36b5c1cc7f7b919aed45834e41243834 Mon Sep 17 00:00:00 2001 From: stevencwilliams Date: Fri, 20 Jan 2012 22:11:28 +0000 Subject: [PATCH] Added example client code for float and long double precisions. --- example/Makefile | 12 +++++++++-- example/example_float.cpp | 38 +++++++++++++++++++++++++++++++++ example/example_long_double.cpp | 38 +++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 example/example_float.cpp create mode 100644 example/example_long_double.cpp diff --git a/example/Makefile b/example/Makefile index a0f1f29..ec48c51 100644 --- a/example/Makefile +++ b/example/Makefile @@ -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 diff --git a/example/example_float.cpp b/example/example_float.cpp new file mode 100644 index 0000000..60f0387 --- /dev/null +++ b/example/example_float.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +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; +} diff --git a/example/example_long_double.cpp b/example/example_long_double.cpp new file mode 100644 index 0000000..304c38f --- /dev/null +++ b/example/example_long_double.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +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; +}