ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
arraymatrixtools.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2017-2020 German Aerospace Center (DLR).
3  * Eclipse ADORe, Automated Driving Open Research https://eclipse.org/adore
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License 2.0 which is available at
7  * http://www.eclipse.org/legal/epl-2.0.
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * Reza Dariani - initial API and implementation
13  ********************************************************************************/
14 
15 /*
16 DaR 2016
17 */
18 #pragma once
19 #include <math.h>
22 #include <dlib/matrix/matrix.h>
23 #include <vector>
24 # define M_PI 3.14159265358979323846 /* pi */
25 
26 
27 namespace adore
28 {
29  namespace mad
30  {
32  {
33  public:
35 
42  template <typename T>
43  static double mean(T *input, int inputSize)
44  {
45  double output = 0.0;
46  for (int i = 0; i < inputSize; i++)
47  {
48  output += input[i];
49  }
50  return output / inputSize;
51  }
58  template <typename T>
59  static double sum(T* input, int inputSize)
60  {
61  double sum = 0;
62  for (int i = 0; i < inputSize; i++)
63  {
64  sum = sum + input[i];
65  }
66  return sum;
67  }
74  template <typename T>
75  static void diff(T * output, T* input, int inputSize)
76  {
77  for (int i = 0; i < inputSize - 1; i++)
78  {
79  output[i] = input[i + 1] - input[i];
80  }
81  }
88  template <typename T>
89  static void sort(T* output, int* outputIndex, T* input, int inputSize)
90  {
91  int tempIndex;
92  double *tempInput;
93  tempInput = new double[inputSize];
94  std::copy(input, input + inputSize, tempInput);
95  double temp;
96  //outputIndex initialization
97  outputIndex[0] = 0;
98  for (int i = 1; i < inputSize; i++)
99  {
100  outputIndex[i] = outputIndex[i - 1] + 1;
101  }
102  //sorting
103  for (int i = 0; i < inputSize; i++)
104  {
105  for (int j = inputSize - 1; j > i; j--)
106  {
107  if (input[j] < input[j - 1])
108  {
109  temp = input[j - 1];
110  tempIndex = outputIndex[j - 1];
111  input[j - 1] = input[j];
112  outputIndex[j - 1] = outputIndex[j];
113  input[j] = temp;
114  outputIndex[j] = tempIndex;
115  }
116  }
117  }
118  std::memcpy(&output[0], &input[0], inputSize * sizeof(double));
119  std::memcpy(&input[0], &tempInput[0], inputSize * sizeof(double));
120  delete [] tempInput;
121  }
122 
131  template <typename T>
132  static bool any(T *input, int inputSize, const char* condition, double ref)
133  {
134  bool check = false;
135  switch( hashit(condition) )
136  {
137  case EQUAL:
138  for (int i = 0; i < inputSize; i++)
139  {
140  if (input[i] == ref)
141  {
142  check = true;
143  break;
144  }
145  }
146  break;
147  case LOWER:
148  for (int i = 0; i < inputSize; i++)
149  {
150  if (input[i] < ref)
151  {
152  check = true;
153  break;
154  }
155  }
156  break;
157  case GREATER:
158  for (int i = 0; i < inputSize; i++)
159  {
160  if (input[i] > ref)
161  {
162  check = true;
163  break;
164  }
165  }
166  break;
167  case LOWER_EQUAL:
168  for (int i = 0; i < inputSize; i++)
169  {
170  if (input[i] <= ref)
171  {
172  check = true;
173  break;
174  }
175  }
176  break;
177  case GREATER_EQUAL:
178  for (int i = 0; i < inputSize; i++)
179  {
180  if (input[i] >= ref)
181  {
182  check = true;
183  break;
184  }
185  }
186  break;
187  default:
188  printf("\n %c", condition[0]);
189  printf("\nError:(any) Please choose a correct condition");
190  printf("\nG for greater, E for equal, L for less");
191  printf("\nGE for greater equal, LE for less equal");
192  break;
193  }
194  return check;
195  }
205  template <typename T>
206  static std::vector<int> find(T *input, int inputSize, const char *condition, double ref)
207  {
208  std::vector<int> output;
209  switch( hashit(condition) )
210  {
211  case EQUAL:
212  for(int i=0; i<inputSize; i++)
213  {
214  if (input[i] == ref)
215  {
216  output.push_back(i);
217  }
218  }
219 
220  break;
221  case GREATER:
222  for(int i=0; i<inputSize; i++)
223  {
224  if (input[i] > ref)
225  {
226  output.push_back(i);
227  }
228  }
229  break;
230  case LOWER:
231  for(int i=0; i<inputSize; i++)
232  {
233  if (input[i] < ref)
234  {
235  output.push_back(i);
236  }
237  }
238  break;
239  case GREATER_EQUAL:
240  for(int i=0; i<inputSize; i++)
241  {
242  if (input[i] >= ref)
243  {
244  output.push_back(i);
245  }
246  }
247  break;
248  case LOWER_EQUAL:
249  for(int i=0; i<inputSize; i++)
250  {
251  if (input[i] <= ref)
252  {
253  output.push_back(i);
254  }
255  }
256  break;
257  default: printf("\nNOT VALID CONDITION FOR adore::mad::ArrayMatrixTools::find"); break;
258  }
259  return output;
260  }
268  template <typename T>
269  static std::vector<int> find(std::vector<T>* input, const char *condition, double ref)
270  {
271  return find (input->data(), input->size(),condition, ref);
272  }
283  template <typename T>
284  static void find(int *output, int *outputSize, T *input, int inputSize, const char *condition, double ref)
285  {
286  auto solution = find (input, inputSize ,condition, ref);
287  std::memcpy(&output[0],&solution.data()[0],solution.size()*sizeof(int));
288  *outputSize = solution.size() ;
289  }
297  template <typename T>
298  static void transpose(T *output, T *input, int input_row, int input_column)
299  {
300  for (int i = 0; i < input_row; i++)
301  {
302  for (int j = 0; j < input_column; j++)
303  {
304  output[j*input_row + i] = input[i*input_column + j];
305  }
306  }
307  }
318  static void matrixMultiplication(double *output, double *input_1, int input_1_row, int input_1_column, double *input_2, int input_2_row, int input_2_column)
319  {
320  if (input_1_column != input_2_row)
321  printf("\n Error, invalid matrix size");
322 
323  for (int i = 0; i < input_1_row*input_2_column; i++)
324  output[i] = 0; //initialization
325 
326  for (int r_1 = 0; r_1 < input_1_row; r_1++)
327  {
328  for (int c_2 = 0; c_2 < input_2_column; c_2++)
329  {
330  for (int c_1 = 0; c_1 < input_1_column; c_1++)
331  {
332  output[(r_1*input_2_column) + c_2] += input_1[(r_1*input_1_column) + c_1] * input_2[(c_1*input_2_column) + c_2];
333  }
334  }
335  }
336  }
344  template <typename T>
345  static void pointwise_multiply(T *output, T *input_1, T *input_2, int inputSize)
346  {
347  for (int i = 0; i < inputSize; i++)
348  {
349  output[i] = input_1[i] * input_2[i];
350  }
351  }
359  template <typename T>
360  static void pieceOfArray(T *output, T *input, int start, int end)
361  {
362  int counter = 0;
363  for (int i = start; i <= end; i++)
364  {
365  output[counter] = input[i];
366  counter++;
367  }
368  }
378  template <typename T>
379  static void sparseDiagonalMatrix(T *sparse, T *input, int output_row, int output_column, int inputSize, int StartingReference)
380  {
381  int offsetRow = 0;
382  int offsetColumn = 0;
383  int counter = 0;
384  if (StartingReference > 0)
385  offsetColumn = StartingReference;
386  if (StartingReference < 0)
387  offsetRow = std::abs(StartingReference);
388 
389  if (inputSize < std::min(output_row - offsetRow, output_column - offsetColumn) || output_row - offsetRow == 0 || output_column - offsetColumn == 0)
390  {
391  printf("\nError, DiagonalSparseMatrix, requested matrix has bigger diagonal than delivered input");
392  return;
393  }
394  if (output_column > output_row)
395  {
396  for (int i = 0; i < output_row; i++)
397  {
398  for (int j = 0; j < output_column; j++)
399  {
400  if (i - offsetRow == j - offsetColumn)
401  {
402  sparse[i*output_column + j] = input[counter + offsetRow];
403  counter++;
404  }
405  else
406  {
407  sparse[i*output_column + j] = 0;
408  }
409  }
410  }
411  }
412  if (output_column <= output_row)
413  {
414  for (int i = 0; i < output_row; i++)
415  {
416  for (int j = 0; j < output_column; j++)
417  {
418  if (i - offsetRow == j - offsetColumn)
419  {
420  sparse[(i*output_column) + j] = input[counter + offsetColumn];
421  counter++;
422  }
423  else
424  {
425  sparse[(i*output_column) + j] = 0;
426  }
427  }
428  }
429  }
430  }
436  static double angle_norm(double x)
437  {
438  x = fmod(x + M_PI, 2 * M_PI);
439  if (x < 0)
440  x += (2 * M_PI);
441  return x - M_PI;
442  }
449  static double unwrap(double prev, double now)
450  {
451  return prev + angle_norm(now - prev);
452  }
458  static bool isNaN(double x)
459  {
460  if(x !=x)
461  {
462  return true;
463  }
464  else
465  {
466  return false;
467  }
468  }
474  template <typename T>
475  static int sign(T x)
476  {
477  if(x>=0.0000) return 1;
478  else return -1;
479  }
486  template <typename T>
487  static double exponentialAverage(T *input, int inputSize)
488  {
489  double sum = 0.0;
490  double wSum = 0.0;
491  double w;
492  double pow ;
493 
494  for(int i=0; i<inputSize;i++)
495  {
496  pow = i-inputSize+1;
497  w = std::exp(pow);
498  wSum += w;
499  sum += w * input[i];
500  }
501  return sum/wSum;
502  }
510  template <typename T>
511  static double exponentialMovingAverage(T *input, int inputSize, double newValue)
512  {
513  double *tmp;
514  tmp = new double [inputSize];
515  std::memcpy(&tmp[0],&input[1],(inputSize-1)* sizeof(double));
516  std::memcpy(&input[0],&tmp[0],(inputSize-1)* sizeof(double));
517  input[inputSize-1] = newValue;
518  delete [] tmp;
519  return exponentialAverage(&input[0], inputSize);
520  }
521 
522  static double aglBetwVecInArcLen(double * v1, double * v2,int dim)
523  {
524  double magV1 = 0;
525  double magV2 = 0;
526  double dotProd = 0;
527 
528  for(int i = 0; i < dim; i++)
529  {
530  magV1 += v1[i] * v1[i];
531  magV2 += v2[i] * v2[i];
532 
533  dotProd += v1[i] * v2[i];
534  }
535 
536  magV1 = std::sqrt(magV1);
537  magV2 = std::sqrt(magV2);
538 
539  return acos(dotProd / (magV1 * magV2));
540  }
541  template <int T>
542  static double aglBetwVecInArcLen(dlib::vector<double,T> v1, dlib::vector<double,T> v2)
543  {
544  return acos(v1.dot(v2) / (v1.length() * v2.length()));
545  }
546  template<int T>
547  static double geoDistance(dlib::vector<double,T> v1, dlib::vector<double,T> v2)
548  {
549  auto temp = v2 - v1;
550 
551  return dlib::length(temp);
552  }
553  static CONDITION hashit (std::string const& inString)
554  {
555  if (inString == "E") return EQUAL;
556  if (inString == "G") return GREATER;
557  if (inString == "L") return LOWER;
558  if (inString == "GE") return GREATER_EQUAL;
559  if (inString == "LE") return LOWER_EQUAL;
560  return LOWER_EQUAL;
561  }
562 
563  };
564  }
565 }
#define M_PI
Definition: arraymatrixtools.h:24
Definition: arraymatrixtools.h:32
static double exponentialMovingAverage(T *input, int inputSize, double newValue)
Definition: arraymatrixtools.h:511
static void pieceOfArray(T *output, T *input, int start, int end)
Definition: arraymatrixtools.h:360
static void transpose(T *output, T *input, int input_row, int input_column)
Definition: arraymatrixtools.h:298
static double mean(T *input, int inputSize)
Definition: arraymatrixtools.h:43
static CONDITION hashit(std::string const &inString)
Definition: arraymatrixtools.h:553
static double sum(T *input, int inputSize)
Definition: arraymatrixtools.h:59
static std::vector< int > find(T *input, int inputSize, const char *condition, double ref)
Definition: arraymatrixtools.h:206
static void pointwise_multiply(T *output, T *input_1, T *input_2, int inputSize)
Definition: arraymatrixtools.h:345
static double aglBetwVecInArcLen(double *v1, double *v2, int dim)
Definition: arraymatrixtools.h:522
static int sign(T x)
Definition: arraymatrixtools.h:475
static void sort(T *output, int *outputIndex, T *input, int inputSize)
Definition: arraymatrixtools.h:89
CONDITION
Definition: arraymatrixtools.h:34
@ EQUAL
Definition: arraymatrixtools.h:34
@ LOWER
Definition: arraymatrixtools.h:34
@ UNDEFINED
Definition: arraymatrixtools.h:34
@ GREATER
Definition: arraymatrixtools.h:34
@ GREATER_EQUAL
Definition: arraymatrixtools.h:34
@ LOWER_EQUAL
Definition: arraymatrixtools.h:34
static double angle_norm(double x)
Definition: arraymatrixtools.h:436
static double geoDistance(dlib::vector< double, T > v1, dlib::vector< double, T > v2)
Definition: arraymatrixtools.h:547
static void diff(T *output, T *input, int inputSize)
Definition: arraymatrixtools.h:75
static void sparseDiagonalMatrix(T *sparse, T *input, int output_row, int output_column, int inputSize, int StartingReference)
Definition: arraymatrixtools.h:379
static bool isNaN(double x)
Definition: arraymatrixtools.h:458
static void find(int *output, int *outputSize, T *input, int inputSize, const char *condition, double ref)
Definition: arraymatrixtools.h:284
static std::vector< int > find(std::vector< T > *input, const char *condition, double ref)
Definition: arraymatrixtools.h:269
static double unwrap(double prev, double now)
Definition: arraymatrixtools.h:449
static void matrixMultiplication(double *output, double *input_1, int input_1_row, int input_1_column, double *input_2, int input_2_row, int input_2_column)
Definition: arraymatrixtools.h:318
static bool any(T *input, int inputSize, const char *condition, double ref)
Definition: arraymatrixtools.h:132
static double exponentialAverage(T *input, int inputSize)
Definition: arraymatrixtools.h:487
static double aglBetwVecInArcLen(dlib::vector< double, T > v1, dlib::vector< double, T > v2)
Definition: arraymatrixtools.h:542
T min(T a, T b, T c, T d)
Definition: adoremath.h:663
string output
Definition: adore_set_goal.py:23
x
Definition: adore_set_goal.py:30
w
Definition: adore_set_pose.py:40
Definition: areaofeffectconverter.h:20