-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdFunctions_DY.cpp
More file actions
37 lines (35 loc) · 1.07 KB
/
Copy pathstdFunctions_DY.cpp
File metadata and controls
37 lines (35 loc) · 1.07 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
//
// stdFunctions_DY.cpp
// GTLC
//
// Created by David Yang on 6/25/18.
// Copyright © 2018 David Yang. All rights reserved.
//
#include <stdio.h>
#include "fuelOpt_DP.hpp"
// Function that adds together two vectors that are the same length and hold doubles
std::vector <double> addVectors(std::vector <double> a, std::vector <double> b){
// std::plus adds together its two arguments:
std::transform (a.begin(), a.end(), b.begin(), a.begin(), std::plus<double>());
return a;
}
// Function to calculate the norm of a std::vector
double l2_norm(std::vector<double> const& u) {
double accum = 0.;
for (int i = 0; i < u.size(); ++i) {
accum += u[i] * u[i];
}
return sqrt(accum);
}
// Function to return a linearly spaced vector (MATLAB style)
std::vector<double> linspace(double a, double b, std::size_t N)
{
double h = (b - a) / static_cast<double>(N-1);
std::vector<double> xs(N);
std::vector<double>::iterator x;
double val;
for (x = xs.begin(), val = a; x != xs.end(); ++x, val += h) {
*x = val;
}
return xs;
}