-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.h
69 lines (57 loc) · 1.97 KB
/
core.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/////////////////////////////////////////////////
// core.h: declaration of some useful functions
// and a class for array manipulation
/////////////////////////////////////////////////
#ifndef CORE_H
#define CORE_H
#include "rapidxml-1.13/rapidxml.hpp"
#include "rapidxml-1.13/rapidxml_utils.hpp"
using namespace rapidxml;
void str_to_val(const char* str, double* val);
void xmlnode_attr_to_val(const xml_node<>* xnode, const char* attr_name, double* val);
void xmlnode_attr_to_val(const xml_node<>* xnode, const char* attr_name, std::string& val);
double** new_2d_array(int n, int m);
void delete_2d_array(double** array, int n);
void save_2d_array(double** array, int n, int m, std::string fname, bool append_flag);
double randf();
// Class arrayops ( = array operations) object simplifis certain array
// operations on arrays of given length n, such as vector arifmetic.
// TODO: maybe generalize to any type with templates?
class arrayops{
int n; // array size
double* tmp; // extra array
public:
arrayops(int n);
arrayops();
~arrayops();
void set_n(int n_);
void print(double* a);
void assign(double* a, const double* a1) const;
double* add(double* a, const double* a1) const;
double* subtract(double* a, const double* a1) const;
double* times(double* a, double b) const;
double* modulus(double* a, double b) const;
double dot(const double* a, const double* a1) const;
double norm(const double * a) const;
double distance(const double* a, const double* a1);
void assign_scalar(double* a, double b) const;
double l1_norm(const double* a) const;
double** new_2d_array(int m) const;
void delete_2d_array(double** array, int m) const;
};
#include <iostream>
using namespace std;
template <typename T>
void print_array (const T* a, int n){
for(int i=0;i<n;i++){
if(i){cout << " ";}
cout << *a++;
}
cout << endl;
}
template <typename T>
void print_array (const T* a, int n, const char* prefix){
cout << prefix;
print_array <T> (a,n);
}
#endif