forked from vedantk/lcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cpp
More file actions
105 lines (92 loc) · 1.86 KB
/
Copy pathcommon.cpp
File metadata and controls
105 lines (92 loc) · 1.86 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* common header impl
*
* Copyright (c) 2009 Vedant Kumar <vminch@gmail.com>
*/
#include "common.hpp"
ushort num_imgs;
num NUM_EPSILON;
void hit_enter() {
dp("\nPress 'enter' or 'return' to continue.");
cin.get();
}
void error(const char* reason) {
dp(reason);
hit_enter();
exit(2);
}
int makedir(const char* path) {
if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) == -1) {
if (errno == EEXIST) return 1;
cout << path << endl;
cout << "Could not create directory: " << path << endl;
exit(-1);
}
}
num sum_of(num* array, uint len) {
num t = NUM_EPSILON;
for (uint q=0; q < len; ++q) {
t += array[q];
}
return t;
}
num square(num x) {
return x * x;
}
num sign(num val) {
if (val > 0) {
return 1;
} else if (val < 0) {
return -1;
} else {
return 0;
}
}
void mat_op(num* lhs, num* rhs, uint len, m_op op, num* result) {
for (uint it=0; it < len; ++it) {
switch (op) {
case mul:
result[it] = lhs[it] * rhs[it];
continue;
case divn:
result[it] = lhs[it] / rhs[it];
continue;
case add:
result[it] = lhs[it] + rhs[it];
continue;
case sub:
result[it] = lhs[it] - rhs[it];
continue;
case pown:
result[it] = pow(lhs[it], rhs[it]);
continue;
}
}
}
void mat_op(num* lhs, num delta, uint len, m_op op, num* result) {
for (uint it=0; it < len; ++it) {
switch (op) {
case mul:
result[it] = lhs[it] * delta;
continue;
case divn:
result[it] = lhs[it] / delta;
continue;
case sub:
result[it] = lhs[it] - delta;
continue;
case pown:
result[it] = pow(lhs[it], delta);
continue;
case add:
result[it] = lhs[it] + delta;
continue;
}
}
}
void del_num_table(num** t, ushort rows) {
for (ushort k=0; k < rows; ++k) {
delete[] t[k];
}
delete t;
}