Deep network
vector.hpp
Go to the documentation of this file.
1#ifndef VECTOR_HPP
2#define VECTOR_HPP
3
4#include <iostream>
5#include "debug.hpp"
6
7using namespace std;
8
9using Real = double;//float;
10
11#ifdef DEBUG
12struct Vector{
13 size_t n;
14 Real* data;
15 Real& operator[](size_t i);
16};
17
18inline Real&
19Vector::operator[](size_t i){
20 assert(i<n);
21 return data[i];
22}
23
24inline Vector
25init_vector(size_t n){
26 Vector v;
27 v.n=n;
28 v.data=new Real[n];
29 return v;
30}
31
32inline void
34 delete[] v.data;
35}
36
37inline bool
38is_null(Vector v){
39 return v.n==0;
40}
41
42static const Vector NullVector={0,nullptr};
43
44#else
45using Vector=Real*;
46
47inline Vector
48init_vector(size_t n){
49 return new Real[n];
50}
51
52inline void
54 delete[] v;
55}
56#endif
57
58inline void
59display(Vector v,size_t n){
60 if(n==0){
61 cout<<"[]"<<endl;
62 return;
63 }
64 cout<<'['<<v[0];
65 for(size_t i=1;i<n;++i){
66 cout<<','<<v[i];
67 }
68 cout<<']'<<endl;
69}
70
71inline size_t
72argmax(Vector v,size_t n){
73 assert(n>0);
74 size_t imax=0;
75 Real vmax=v[0];
76 for(size_t i=1;i<n;++i){
77 if(v[i]>vmax){
78 vmax=v[i];
79 imax=i;
80 }
81 }
82 return imax;
83}
84
85inline size_t
86indice2(size_t i,size_t j,size_t nj){
87 return i*nj+j;
88}
89
90inline size_t
91indice3(size_t i,size_t j,size_t k,size_t nj,size_t nk){
92 return (i*nj+j)*nk+k;
93}
94
95inline size_t
96indice4(size_t i,size_t j,size_t k,size_t l,size_t nj,size_t nk,size_t nl){
97 return ((i*nj+j)*nk+k)*nl+l;
98}
99
100#endif
#define assert(cond)
Definition: debug.hpp:11
void display(Vector v, size_t n)
Definition: vector.hpp:59
size_t indice4(size_t i, size_t j, size_t k, size_t l, size_t nj, size_t nk, size_t nl)
Definition: vector.hpp:96
size_t argmax(Vector v, size_t n)
Definition: vector.hpp:72
void delete_vector(Vector v)
Definition: vector.hpp:53
double Real
Definition: vector.hpp:9
Vector init_vector(size_t n)
Definition: vector.hpp:48
size_t indice3(size_t i, size_t j, size_t k, size_t nj, size_t nk)
Definition: vector.hpp:91
size_t indice2(size_t i, size_t j, size_t nj)
Definition: vector.hpp:86
Real * Vector
Definition: vector.hpp:45