Initializer element is not a compile-time constant when multiplying constants, C++ - c++

I have this code here
//GLOBAL
// Power Demand in St John's per year [GJ/yr]
double P_year = 14367865E9;
double s_year = 365*24*60*60;// Seconds in a year [s]
// Power Demand in St John's per second [GJ/yr]
double P_s = P_year*(1/s_year);
//electric permitivitty of free space [s4A2/m3kg]
double epsilon = 8.85418782E-12;
int main(){
// Energy Required for 6hrs demand [J]
double E = P_s * 60*60*6;
// Voltage Difference accross capacitor [V]
double V = 230E3;
// Capacitance
double C = 2*E/pow(V,2);
printf("%f %f \n", E, C);
}
I get the following error:
Capacitor.c:11:20: error: initializer element is not a compile-time constant
double P_s = P_year*(1/s_year);
I do not understand why it won't just multiply it.
Thank you guys for your help.

Related

3D vector angle calculation formula returns nan (acos argument bigger than 1 somehow... )

Because of test1 and test2 i know that the issue here is that the argument of acos is larger than 1 and thus i get nAn as result. But what exactly is wrong with my calculation?
Vectors are:
v1(3,4,0),
v2(0,1,0)
Expected result for the angle is 36.87°
double Vector::angle(const Vector& input) const
{
double test1 = sqrt(this->length() * input.length()); //equals 2.23607
double test2 = this->dotProd(input); //equals 4
double result = acos(this->dotProd(input) / sqrt(this->length() * input.length()));
return result;
}
But what exactly is wrong with my calculation?
The sqrt at the denominator here:
double result = acos(this->dotProd(input) / sqrt(this->length() * input.length()));
It should be:
double result = std::acos(this->dotProd(input) / (this->length() * input.length()));
You could also infer that from dimentional analysis. If you assume the vector
represents, say, the displacement between two points in meters (m):
The vector length has unit [m];
The dot product has unit [m * m];
So you pass acos a parameter whose dimension is [m], while it should be
dimensionless.

output is NaN for standard deviation - C++

I have a process in my client program that iterates through a custom vector, and performs some calculations to calculate standard deviation. It converts a wind speed from m/s to km/h, so its *3.6 to convert. For my sigma value, and of course my standard deviation which requires the sigma value, my output is NaN and I can't understand why this is the case.
Main.cpp:
float sum;
float convertedspeed[windlog.size()];
float sigma;
float averagespeed;
//float conversion;
float sd;
int nrofel;
ofstream ofile("testoutput.csv");
ofile << "WAST, S, \n";
for(int i = 0; i < windlog.size(); i++){
nrofel = windlog.size();
convertedspeed[i] = (windlog[i].speed*3.6);
sum += convertedspeed[i];
averagespeed = (sum/nrofel);
sigma += (convertedspeed[i] - averagespeed)*(convertedspeed[i] - averagespeed);
sd = sqrt(sigma/(nrofel - 1));
All my values are okay except when it gets to sigma so I expect its going wrong there. What could be the issue?
You must initialize variables before using.
Strictly speaking, in this case, you don't have to initialize nrofel, averagespeed, and sd because they are assigned some values in the loop without being read before assignment.
On the other hand, you must initialize sum and sigma because their values are read before assignment.

How to find (Q, R ) from SuiteSparseQR_factorization object?

In C++ interface of SuiteSparse, I can use
SuiteSparseQR_factorization <double> *QR;
QR = SuiteSparseQR_factorize(A) ;
to calculate QR decomposition of matrix A so that I can reuse QR for further calculation. But I wonder can I get the real Q,R directly from
this QR object?
SuiteSparse is awesome, but the interface can be confusing. Unfortunately, the methods that involve the SuiteSparseQR_factorization struct, which appear to be the most convenient, haven't worked so well for me in practice. For instance, using SuiteSparseQR_factorize and then SuiteSparseQR_qmult with a sparse matrix input argument actually converts it to a dense matrix first, which seems completely unnecessary!
Instead, use
template <typename Entry> SuiteSparse_long SuiteSparseQR
(
// inputs, not modified
int ordering, // all, except 3:given treated as 0:fixed
double tol, // only accept singletons above tol
SuiteSparse_long econ, // number of rows of C and R to return; a value
// less than the rank r of A is treated as r, and
// a value greater than m is treated as m.
int getCTX, // if 0: return Z = C of size econ-by-bncols
// if 1: return Z = C' of size bncols-by-econ
// if 2: return Z = X of size econ-by-bncols
cholmod_sparse *A, // m-by-n sparse matrix
// B is either sparse or dense. If Bsparse is non-NULL, B is sparse and
// Bdense is ignored. If Bsparse is NULL and Bdense is non-NULL, then B is
// dense. B is not present if both are NULL.
cholmod_sparse *Bsparse,
cholmod_dense *Bdense,
// output arrays, neither allocated nor defined on input.
// Z is the matrix C, C', or X
cholmod_sparse **Zsparse,
cholmod_dense **Zdense,
cholmod_sparse **R, // the R factor
SuiteSparse_long **E, // size n; fill-reducing ordering of A.
cholmod_sparse **H, // the Householder vectors (m-by-nh)
SuiteSparse_long **HPinv,// size m; row permutation for H
cholmod_dense **HTau, // size nh, Householder coefficients
// workspace and parameters
cholmod_common *cc
) ;
This method will perform the factorization and then, optionally, output (among other things) R, the matrix product Z = Q^T * B (or its transpose -- B^T * Q), or the solution of a linear system. To get Q, define B as the identity matrix. Here's an example to get Q and R.
cholmod_common Common, * cc;
cc = &Common;
cholmod_l_start(cc);
cholmod_sparse *A;//assume you have already defined this
int ordering = SPQR_ORDERING_BEST;
double tol = 0;
Long econ = A->nrow;
int getCTX = 1;// Z = (Q^T * B)^T = B^T * Q
cholmod_sparse *B = cholmod_l_speye(A->nrow, A->nrow, CHOLMOD_REAL, cc);//the identity matrix
cholmod_sparse *Q, *R;//output pointers to the Q and R sparse matrices
SuiteSparseQR<double>(ordering, tol, econ, getCTX, A, B, NULL, &Q, NULL, &R, NULL, NULL, NULL, NULL, cc);
If you want any of the other outputs to perform subsequent operations without the use of an explicitly formed Q and/or R, then you need to substitute the NULL's for additional pointers and then make calls to SuiteSparseQR_qmult.

Implementing Exponential Moving Average in C++

I am developing a small trading robot as an exercise. He receives stock prices day after day (represented as iterations).
Here's what my Trade class looks like:
class Trade
{
private:
int capital_;
int days_; // Total number of days of available stock prices
int daysInTrading_; // Increments as days go by.
std::list<int> stockPrices_; // Contains stock prices day after day.
int currentStock_; // Current stock we are dealing with.
int lastStock_; // Last stock dealt with
int trend_; // Either {-1; 0; 1} depending on the trend.
int numOfStocks_; // Number of stocks in our possession
int EMA_; // Exponential Moving Average
int lastEMA_; // Last EMA
public:
// functions
};
As you can see from my last two attributes, I wish to implement an Exponential Moving Average as part of a Trend Following Algorithm.
But I think I didn't quite understand how to implement it; here's my calcEMA function that simply calculates the EMA:
int Trade::calcEMA()
{
return ((this->currentStock_ - this->lastEMA_
* (2/(this->daysInTrading_ + 1)))
+ this->lastEMA_);
}
But when my stock values (passed in a file) are like such:
1000, 1100, 1200, 1300, 1400, 1500, 1400, 1300, 1200, 1100, 1000
As to make sure my EMA makes sense, and well... it does not !
Where did I go wrong on the operation?
Aditionally, what value should I give lastEMA if it's the first time I call calcEMA?
I believe that you are missing a parentheses in the "calcEMA" function. How about breaking the expression up into smaller expressions with temporary variables to hold intermediate results like this?
int Trade::calcEMA()
{
auto mult = 2/(timePeriod_ + 1);
auto rslt = (currentStock_ - lastEMA_) * mult + lastEMA_;
return rslt;
}
Also, as user PaulMcKenzie pointed out in the comment on your question, you are using the integer to do floating point calculation. You may consider using float or double to avoid possible truncation.
Here are my suggestions:
  An EMA like yours is defined for a time period. While daysInTrading is less or equal to timePeriod, lastEMA should be set to a normal average.
  Once daysInTrading is greater than your timePeriod you can start calling your "calcEMA" function with initialized lastEMA.
  Please, remember to update lastEMA after each call to the "calcEMA" function.
Here is my code for you:
#include <vector>
#include <list>
#include <iostream>
// calculate a moving average
double calcMA (double previousAverage,
unsigned int previousNumDays,
double newStock) {
auto rslt = previousNumDays * previousAverage + newStock;
return rslt / (previousNumDays + 1.0);
}
// calculate an exponential moving average
double calcEMA (double previousAverage,
int timePeriod,
double newStock) {
auto mult = 2.0 / (timePeriod + 1.0);
auto rslt = (newStock - previousAverage) * mult + previousAverage;
return rslt;
}
class Trade {
unsigned int timePeriod_ = 5;
double lastMA_ = 0.0;
std::list<double> stockPrices_;
public:
void addStock (double newStock) {
stockPrices_.push_back(newStock);
auto num_days = stockPrices_.size();
if (num_days <= timePeriod_)
lastMA_ = calcMA(lastMA_, num_days - 1, newStock);
else
lastMA_ = calcEMA(lastMA_, num_days - 1, newStock);
}
double getAverage() const { return lastMA_; }
};
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
int main() {
std::vector<double> stocks =
{1000, 1100, 1200, 1300, 1400, 1500,
1400, 1300, 1200, 1100, 1000};
Trade trade;
for (auto stock : stocks)
trade.addStock(stock);
std::cout << "Average: " << trade.getAverage() << std::endl;
return 0;
}
The operation is wrong, as you noticed.
Disclaimer I got this algorithm from wikipedia, and as such might no be accurate. Here (page 3) might be a better one, but I can't judge, I never used those algorithms and so have no idea what I'm talking about :)
c(EMA) = y(EMA) + a * (c(price) - y(EMA))
c(EMA) is current EMA
y(EMA) is previous EMA
a is some "random" value between 0 and 1
c(price) is current price
But you did almost the same thing:
c(EMA) = (c(price) - y(EMA) * b) + y(EMA)
I don't know why you did 2 / daysInTrading_ + 1, but this will not always be a value between 0 and 1 (actually, it might even be most of the time 0, because those are all intergers).
You put a parenthesis at the wrong place (after b, and not after y(EMA)).
So the operation will now look like this:
lastEMA_ + 0.5 * (currentStock_ - lastEMA_)
For the first lastEMA_, according to Wikipedia:
S1 is undefined. S1 may be initialized in a number of different ways, most commonly by setting S11 [First element in the list], though other techniques exist, such as setting S1 to an average of the first 4 or 5 observations.
The importance of the S1 initialisations effect on the resultant moving average depends on α; smaller α values make the choice of S1 relatively more important than larger α values, since a higher α discounts older observations faster.
There are generally two accepted forms of EMA.
The traditional:
m = 2/(1+n) // where n >= 1
EMA = m * currentPrice + (1-m) * previousEMA
rf the Wilder:
m = 1/n // where n >= 1
EMA Wilder = m * currentPrice + (1-m) * previousEMA

help with secant root finding C++

Can someone explain to me how I would use the secant method to find the root of an equation?
The equation is: ( v / b ) ^2sin(alpha)= kr * Ts^4 +Uc *Ts -q
and I have to find Ts. I have all the other info but am confused on what I'm supposed to do with the seccant method. Any help would be greatly appreciated.
Here is my code so far:
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
void secant(double, double, double, double, double, double, double);
int main()
{
double kr, uc, q, b, radians;
const double PI = 4.0 * atan(1.0);
ifstream datain("shuttle.txt");
ofstream dataout("results.txt");
datain >> kr >> uc >> q >> b;
int velocity = 16000;
double angle = 10;
for (int velocity = 16000; velocity <= 17500; velocity += 500) {
for (int angle = 10; angle <= 70; angle += 15) {
radians = angle * PI / 180;
cout << velocity << endl;
cout << radians << endl;
cout << angle << endl;
secant(angle, radians, velocity, kr, uc, q, b);
}
}
getchar();
}
void secant(double angle, double radians, double velocity, double kr, double uc,
double q, double b)
{
}
The Wikipedia article on the Secant Method includes a nice layout of successive x_n values, which I'm cut-n-pasting here:
...
You need your secant method to iteratively calculate these x_n values, until either (a) you realize the method is diverging, and you cannot find a solution or (b) your successive x_n values are changing by small enough amounts that you can happily call the result a root.
So you'll need a function f that you can call to calculate your equation:
double f(double Ts, double v, double b, double alpha, double kr, double Uc, double q) {
double first = pow(v/b, 2.0) * sin(alpha);
double second = kr * pow(Ts, 4.0) + Uc * Ts - q;
return first - second;
}
Be sure to check the order of operations. :) Writing equations in HTML is always iffy.
Next you need to write a loop that checks for the exit conditions:
x_0 = /* some guess */
x_1 = x_0 + .01 /* or similar */
while ( (fabs(x_0 - x_1) > EPSILON) && (fabs(x_0 - x_1) < DIVERGENCE) ) {
x_new = x_1 - f(x_1, /* rest */) * (x_1 - x_0) / (f(x_1, /* rest */) - f(x_0, /* rest */));
x_0 = x_1;
x_1 = x_new;
}
You might consider hiding all the arguments to f() that aren't being solved for via a macro. That would help make sure you get all the arguments in the correct order.
And definitely consider solving much simpler functions like x^2 - 17 == 0 before jumping into your multivariate function. (It'd also remove the confusing double-inner-loop you've got now. That's just a recipe for multiplying any errors by a few hundred times. :)
[ There is actually an analytic method for solving the quartic (which is admittedly fairly involved), but as this is homework exercise, you presumably want the secant numerical method. For extra marks, you might want to check that the analytic results agree!]
I assume you have already checked Wikipedia. It shows that the numerical iteration is:
x[n] = x[n-1] - f(x[n-1]) * (x[n-1] - x[n-2])/(f(x[n-1] - f(x[n-2]));
Start with x[0], x[1] suitably chosen - e.g. use a guess from graphing in Excel.
To do the iteration nicely, you probably want to abstract f(x). You can use a function pointer, a functor or just an abstract class.
class Function
{
public:
virtual double evaluate(double x) const = 0;
};
double findRootUsingSecant(const Function& function, double x0, double x1);
Pass in an instance of a class which implements evaluate() by computing your formula, and implement the above iteration. Make sure to terminate the iteration on some suitable conditions.