Anonymous avatar Anonymous committed 97c54e0

Added the codes for the C/C++ Scientific article

Comments (0)

Files changed (10)

c_scientific_article/README

+The source code in this directory are meant to be used as resources for the article 
+"C/C++ Scientific Programming Libraries and Tools" to be published in Linux Journal tentatively in May, 2012.
+(http://echorand.me/writings/)

c_scientific_article/array_demo.cc

+/*array_demo.cc*/
+/* Simple demonstration of using Array 
+   in Blitz++*/
+
+#include <blitz/array.h>
+
+using namespace blitz;
+
+int main()
+{
+
+ 
+  cout << ">>>> 1-D Array Demonstration >>>>" << endl << endl;
+
+
+  Array<float,1> a(5);
+  a=1,2,3,4,5;
+  cout << "a = " << a <<endl << endl;
+
+  Array<float,1> b(5);
+  b=2,1,3,4,1;
+  cout << "b = " << b <<endl << endl;
+
+  cout << " >> Basic Arithmetic Operations >>" << endl << endl;
+
+  Array<float,1> c(5);
+  c = a+b;
+  cout << "c = a+b = " << c <<endl << endl;
+
+  c = a*b;
+  cout << "c = a*b = " << c <<endl << endl;
+  
+  c = a/b;
+  cout << "c = a/b = " << c <<endl << endl;
+
+  cout << ">>>> 2-D Array Demonstration >>>>" << endl << endl;
+
+  Array<float,2> A(3,3);
+  A = 1, 2, 3,
+    3, 5, 1,
+    1, 1, 4;
+
+  cout << "A = " << A << endl;
+
+    Array<float,2> B(3,3);
+  B = 1, 2, 3,
+    3, 5, 1,
+    1, 1, 4;
+
+  cout << "B = " << B << endl; 
+
+    cout << " >> Basic Arithmetic Operations >>" << endl << endl;
+
+  Array<float,2> C(3,3);
+  C = A+B;
+  cout << "C = A+B = " << C <<endl << endl;
+
+  C = A*B;
+  cout << "C = A*B = " << C <<endl << endl;
+  
+  C = A/B;
+  cout << "c = A/B = " << C <<endl << endl;
+
+  return 0;
+}

c_scientific_article/array_dynamic.cc

+/*array_blitz.cc*/
+
+/* Dynamic Array objects using Blitz++*/
+
+#include <blitz/array.h>
+
+using namespace blitz;
+
+int main()
+{
+  int n;
+
+ 
+  cout << ">>>> Dynamic 1-D Array Demonstration >>>>" << endl << endl;
+
+  Array<float,1> a;
+  cout << "Enter the number of elements:: ";
+  cin >> n;
+
+  /* Resize the array */
+  a.resize(n);
+
+  /* Input the array*/
+  for(int i=0;i<n;i++)
+    cin >> a(i); /* uses the  () operator to refer each element*/
+  
+  cout << "a = " << a <<endl << endl;
+
+
+  cout << ">>>> Dynamic 2-D Array Demonstration >>>>" << endl << endl;
+
+  Array<float,2> A;
+  cout << "Enter the number of elements in the two dimensions:: ";
+  int r,c;
+  cin >> r >> c;
+
+  /* Resize the matrix */
+  A.resize(r,c);
+
+  /* Input the array*/
+  for(int i=0;i<r;i++)
+    {
+      for(int j=0;j<c;j++)
+	cin >> A(i,j); /* uses the  () operator to refer each element*/
+    }
+  
+  cout << "A = " << A <<endl << endl;
+
+
+  return 0;
+}

c_scientific_article/chdemo.c

+#!/bin/ch
+#include<stdio.h>
+#include<numeric.h>
+
+int main(int argc, char **argv)
+{
+  array double a[5]={1.4,1.5,9.1,1.3,4.1};
+
+  printf(a);
+  printf("\n");
+  return 0;
+}

c_scientific_article/compile_blitz.sh

+#!/bin/bash
+g++ $1 -lblitz `pkg-config blitz --libs --cflags`

c_scientific_article/gsl_fmin.c

+/*Listing-4: gsl_fmin.c*/
+/* Demonstration of using the function minimizing features
+   in GSL */
+
+#include <stdio.h>
+#include <gsl/gsl_errno.h>
+#include <gsl/gsl_math.h>
+#include <gsl/gsl_min.h>
+     
+/* Function: 2x^2 + 4x having a minimum at x=-1*/
+double fn_1 (double x, void * params)
+{
+  return 2*x*x + 4*x;
+}
+     
+int main ()
+{
+  int status;
+  int iter = 0, max_iter = 100; /*Max. number of iterations*/
+  const gsl_min_fminimizer_type *T;
+  gsl_min_fminimizer *s;
+  double m = 0.7; /* Starting point for the search*/
+  double a = -4.0, b = 1.0; /* The interval in which the minimum lies*/
+  gsl_function F;
+     
+  F.function = &fn_1; /* Function to Minimize*/
+  F.params = 0;
+     
+  T = gsl_min_fminimizer_goldensection; /*Set the minimization algorithm - Uses Golden Section*/
+  s = gsl_min_fminimizer_alloc (T); /* Initialize the minimizer*/
+  gsl_min_fminimizer_set (s, &F, m, a, b); /*Set up the minimizer*/
+     
+  printf ("Using %s method\n", gsl_min_fminimizer_name (s));
+  printf ("%5s [%9s, %9s] %9s \n","iter", "lower", "upper", "min", "err", "err(est)");
+  printf ("%5d [%.7f, %.7f] %.7f \n",  iter, a, b, m);
+
+  /* Set up the iterative minimization procedure*/
+     
+  do
+    {
+      iter++;
+      status = gsl_min_fminimizer_iterate(s);
+     
+      m = gsl_min_fminimizer_x_minimum (s);
+      a = gsl_min_fminimizer_x_lower (s);
+      b = gsl_min_fminimizer_x_upper (s);
+     
+      status = gsl_min_test_interval (a, b, 0.001, 0.0);
+     
+      if (status == GSL_SUCCESS)
+	printf ("Converged:\n");
+     
+      printf ("%5d [%.7f, %.7f] %.7f\n",iter, a, b, m);
+    } while (status == GSL_CONTINUE && iter < max_iter);
+     
+  gsl_min_fminimizer_free (s);
+     
+  return status;
+}

c_scientific_article/gsl_sort.c

+/*Listing-3: gsl_sort.c*/
+
+/* Demonstration of GSL's sorting functions
+ * Also uses the random number generation feature
+ */
+
+#include <stdio.h>
+#include <gsl/gsl_vector.h> /*For Vectors*/
+#include <gsl/gsl_rng.h> /* For Random numbers*/
+     
+int main ()
+{
+  int i,n;
+
+  /* Setup the Random number generator*/
+  const gsl_rng_type * T;
+  gsl_rng * r;
+  gsl_rng_env_setup();
+  T = gsl_rng_default;
+  r = gsl_rng_alloc (T);     
+       
+  
+  printf("Number of elements in the vector:: ");
+  scanf("%d",&n);
+  
+  /* Allocate the vector of the specified size*/
+  gsl_vector * v = gsl_vector_alloc (n);
+
+  /* Set the elements to a uniform random number in [0,1]*/
+  for (i = 0; i < n; i++)
+    {
+      gsl_vector_set (v, i, gsl_rng_uniform (r));
+    }
+       
+  /* Print the vector*/
+  printf("(Hopefully) Unsorted Vector:: ");
+  for (i = 0; i < n; i++)
+    {
+      printf ("%g ", gsl_vector_get (v, i));
+    }
+
+  printf("\n");
+
+  /* Sort the vector*/
+  gsl_sort_vector(v);
+
+  /* Print the sorted vector*/
+  printf("Sorted Vector::               ");
+  for (i = 0; i < n; i++)
+    {
+      printf ("%g ", gsl_vector_get (v, i));
+    }
+  printf("\n");
+
+
+  
+  /* Allocate a large vector*/
+  gsl_vector * v_large = gsl_vector_alloc (10000);
+  
+  /* Set the elements to a uniform random number in [0,1]*/
+  for (i = 0; i < 10000; i++)
+    {
+      gsl_vector_set (v_large, i, gsl_rng_uniform (r));
+    }
+
+
+  /* Find the 100 largest numbers from the above vector*/
+  double *largest = malloc(10*sizeof(double));
+  gsl_sort_vector_largest (largest, 10, v_large);
+
+  printf("\n\n10 largest numbers:: \n\n");
+  
+  /* Print the 10 largest*/
+  for (i = 0; i < 10; i++)
+    printf("%g ",largest[i]);
+  printf("\n\n");
+
+  gsl_vector_free (v);
+  free(largest);
+
+  return 0;
+}

c_scientific_article/gsl_vector.c

+/*Listing-1: gsl_vector.c*/
+
+/* Simple demo of the vector support in GSL
+ *  Also uses the random number generation feature
+ */
+
+#include <stdio.h>
+#include <gsl/gsl_vector.h> /*For Vectors*/
+#include <gsl/gsl_rng.h> /* For Random numbers*/
+     
+int main ()
+{
+  int i,n;
+
+  /* Setup the Random number generator*/
+  const gsl_rng_type * T;
+  gsl_rng * r;
+  gsl_rng_env_setup();
+  T = gsl_rng_default;
+  r = gsl_rng_alloc (T);     
+       
+  
+  printf("Number of elements in the vector:: ");
+  scanf("%d",&n);
+  
+  /* Allocate the vector of the specified size*/
+  gsl_vector * v = gsl_vector_alloc (n);
+
+  /* Set the elements to a uniform random number in [0,1]*/
+  for (i = 0; i < n; i++)
+    {
+      gsl_vector_set (v, i, gsl_rng_uniform (r));
+    }
+       
+  /* Print the vector*/
+  for (i = 0; i < n; i++)
+    {
+      printf ("v_%d = %g\n", i, gsl_vector_get (v, i));
+    }
+     
+  gsl_vector_free (v);
+
+  return 0;
+}

c_scientific_article/gsl_vector_ops.c

+/*Listing-2: gsl_vector_ops.c*/
+
+/* Vector operations in GSL
+ * Also uses the random number generation feature
+ */
+
+#include <stdio.h>
+#include <gsl/gsl_vector.h> /*For Vectors*/
+#include <gsl/gsl_rng.h> /* For Random numbers*/
+     
+int main ()
+{
+  int i,n;
+
+  /* Setup the Random number generator*/
+  const gsl_rng_type * T;
+  gsl_rng * r;
+  gsl_rng_env_setup();
+  T = gsl_rng_default;
+  r = gsl_rng_alloc (T);     
+       
+  
+  printf("Number of elements in the vector:: ");
+  scanf("%d",&n);
+  
+  /* Allocate the vectors of the specified size*/
+  gsl_vector * v1 = gsl_vector_alloc (n);
+  gsl_vector * v2 = gsl_vector_alloc (n);
+
+  /* Set the elements to a uniform random number in [0,1]*/
+  for (i = 0; i < n; i++)
+    {
+      gsl_vector_set (v1, i, gsl_rng_uniform (r));
+      gsl_vector_set (v2, i, gsl_rng_uniform (r));
+    }
+       
+  /* Print the vector*/
+  printf("V1:: ");
+  for (i = 0; i < n; i++)
+    {
+      printf ("%g ", gsl_vector_get (v1, i));
+    }
+  printf("\n");
+
+  printf("V2:: ");
+  for (i = 0; i < n; i++)
+    {
+      printf ("%g ", gsl_vector_get (v2, i));
+    }
+  printf("\n\n");
+
+  printf(">>> Vector Operations >>> \n\n");
+
+
+  /* v1+v2 gets stored in v1*/
+  gsl_vector_add(v1,v2);
+
+  printf("V1+V2:: ");
+  for (i = 0; i < n; i++)
+    {
+      printf ("%g ", gsl_vector_get (v1, i));
+    }
+  printf("\n");
+
+
+  /* v1-v2 gets stored in v1*/
+  gsl_vector_sub(v1,v2);
+
+  printf("V1-V2:: ");
+  for (i = 0; i < n; i++)
+    {
+      printf ("%g ", gsl_vector_get(v1, i));
+    }
+  printf("\n");
+  
+    
+  gsl_vector_free (v1);
+  gsl_vector_free (v2);
+
+  return 0;
+}

c_scientific_article/normal_demo.cc

+/*normal_demo.c*/
+
+/* Using the Uniform Random number Generator*/
+
+#include <random/normal.h>
+#include <blitz/array.h>
+
+using namespace blitz;
+using namespace ranlib;
+
+Array<double,1> randompool_unform(int n);
+
+/* Returns a pool of n uniformly distributed random numbers*/
+Array<double,1> randompool_uniform(int n)
+{
+  /* Uniform Normal distribution with mean 0 and standarad deviation 1*/
+  Normal<double> rnd_normal(0,1);
+
+  /* Setup the seed*/
+  rnd_normal.seed((unsigned int)time(0));
+
+  /* Declare an array and create the pool*/
+  Array<double,1> rnd_array(n);
+  for(int i=0;i<n;i++)
+    rnd_array(i) = rnd_normal.random();   
+
+  /* return */
+  return rnd_array;
+}
+
+
+int main()
+{
+  int n;
+  cout << "Number of unifromly distributed random integers? :: " ;
+  cin >> n;
+
+  Array<double,1> rnd_array;
+  rnd_array.resize(n);
+
+  /* Call the random pool*/
+  rnd_array = randompool_uniform(n);
+  
+  /* print each element individually to facilitate
+     plotting*/
+  for(int i=0;i<n;i++)
+    cout << rnd_array(i) << endl;
+  return 0;  
+    
+}
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.