Anyone can show me an example of the function dspr in openblas - rcpp

I am trying to use the function dspr in Openblas with Rcpp.
the purpose of dspr is:
A := alpha*x*x**T + A
In my case, firstly I define A as a matrix with all the elements are 0, alpha=1, x=(1,3), so, the final matrix A should be {(1,3),(3,9)}, but I never get the right result, I set the parameters as follow:
cblas_dspr(CblasColMajor,CblasUpper,2, 1, &(x[0]),1, &(A[0]));
Can anyone tell me how to set the right parameters of dspr? Thanks.

The file /usr/include/cblas,h on my machine shows the following signature for the C interface of the BLAS:
void cblas_dspr(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo,
const int N, const double alpha, const double *X,
const int incX, double *Ap);
Try that. You get the beginning of Rcpp vectors via x.begin() or via &(x[0]) as you did.
There is nothing pertinent to Rcpp here though.

Repeated from your own post: The BLAS dyadic product performs
A := alpha*x*x' + A
So A would need to be initialized with zero values.
In addition do not forget that A is an upper triangular matrix.
For further reading I recommend these links:
https://developer.apple.com/library/ios/documentation/Accelerate/Reference/BLAS_Ref/Reference/reference.html
https://github.com/foundintranslation/Kaldi/blob/master/tools/ATLAS/interfaces/blas/C/src/cblas_dspr.c
However, you wanted an example. Here goes:
/** dspr_demo.cpp */
#include <cblas.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv)
{
int n=2;
double* x = (double*)malloc(n*sizeof(double));
double* upperTriangleResult = (double*)malloc(n*(n+1)*sizeof(double)/2);
for (int j=0;j<n*(n+1)/2;j++) upperTriangleResult[j] = 0;
x[0] = 1; x[1] = 3;
cblas_dspr(CblasRowMajor,CblasUpper,n,1,x,1,upperTriangleResult);
double*& A = upperTriangleResult;
cout << A[0] << "\t" << A[1] << endl << "*\t" << A[2] << endl;
free(upperTriangleResult); free(x);
return EXIT_SUCCESS;
}

Related

How to make an array of pointers and make the user enter the size of it?

I want to make an array, and inside this array there are pointers, like this:
int *arrp[size]; and I want the user to enter the size of it.
I tried to do this:
#include <iostream>
using namespace std;
int main ()
{
int size;
cout << "Enter the size of the array of pointers" << endl;
cin >> size;
int *arrp[size];
return 0;
}
but this doesn't work.
I also tried to do this:
#include <iostream>
using namespace std;
int main ()
{
int size;
cout << "Enter the size of the array of pointers" << endl;
cin >> size;
int* arrp[] = new int[size];
return 0;
}
also doesn't work, can someone help?
The error of the first code is that the size must be constant, I tried to fix that by writing the 2nd code but it gives an error for the word "new" in line 9:
E0520 initialization with '{...}' expected for aggregate object
and another error for the size in the same line:
C2440 'initializing': cannot convert from 'int *' to 'int *[]'
To make an array of pointers you should type: int** arr = new int*[size]
we type 2 stars '*', the first mean a pointer to an integer, the second means a pointer to the pointer to the integer, and then we make a place in the memory for those pointers by typing = new int*[size], you can use this as a 2D array that stored in the heap (not the stack) go to this website to know the difference: https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/.
to know more about how to use an array of pointers to a pointer to an integers you can see this video: https://www.youtube.com/watch?v=gNgUMA_Ur0U&ab_channel=TheCherno.

Convert string with 6 digits before decimal to Double

I have a string with the value 788597.31 and I am converting this value to double but when I print the variable only 788597 is displayed. I have used std::stod(string) and even stringstream but everytime I get the same previous value. Can anybody help me with this?
I want to store this string value in a double varaible.
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
string s="788597.31";
double d=stod(s);
cout<<d<<" ";
stringstream g;
double a;
g<<s; g>>a;
cout<<a;
return 0;
}
The problem is in how you are printing your result, not in the string parsing. This program:
#include <iostream>
using namespace std;
int main() {
cout << 788597.31 << endl;
return 0;
}
also prints 788597.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setprecision(10) << 788597.31 << endl;
return 0;
}
prints 788597.31
If you want to see more than the default 6 significant digits your program needs to say so.

C++11: how to use accumulate / lambda function to calculate the sum of all sizes from a vector of string?

For a vector of strings, return the sum of each string's size.
I tried to use accumulate, together with a lambda function (Is it the best way of calculating what I want in 1-line?)
Codes are written in wandbox (https://wandbox.org/permlink/YAqXGiwxuGVZkDPT)
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> v = {"abc", "def", "ghi"};
size_t totalSize = accumulate(v.begin(), v.end(), [](string s){return s.size();});
cout << totalSize << endl;
return 0;
}
I expect to get a number (9), however, errors are returned:
/opt/wandbox/gcc-head/include/c++/10.0.0/bits/stl_numeric.h:135:39: note: 'std::__cxx11::basic_string' is not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>'
135 | __init = _GLIBCXX_MOVE_IF_20(__init) + *__first;
I want to know how to fix my codes? Thanks.
That's because you do not use std::accumulate properly. Namely, you 1) did not specify the initial value and 2) provided unary predicate instead of a binary. Please check the docs.
The proper way to write what you want would be:
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> v = {"abc", "def", "ghi"};
size_t totalSize = accumulate(v.begin(), v.end(), 0,
[](size_t sum, const std::string& str){ return sum + str.size(); });
cout << totalSize << endl;
return 0;
}
Both issues are fixed in this code:
0 is specified as initial value, because std::accumulate needs to know where to start, and
The lambda now accepts two parameters: accumulated value, and the next element.
Also note how std::string is passed by const ref into the lambda, while you passed it by value, which was leading to string copy on each invocation, which is not cool

ambiguous call to overloaded function with "bind" when i add header file"boost/function"

I write a test of boost::function.
These codes are working.
#include <iostream>
#include <boost/lambda/lambda.hpp>
#include <boost/bind.hpp>
//#include <boost/function.hpp>
#include <boost/ref.hpp>
using namespace std;
using namespace boost;
template<typename FUN,typename T>
T fun( FUN function, T lhs, T rhs ){
cout << typeid(function).name() << endl;
return function(lhs,rhs);
}
int add4(int a, int b, int c){
return a + b + c;
}
int main(){
cout << fun(bind(add4,2,_1,_2),1,4) << endl;
system("pause");
}
But when i add header file "boost/funcation"
VS2012 prompts me it.
error C2668: 'std::bind' : ambiguous call to overloaded function.
Don't import both std and boost namespaces into the global namespace, to avoid such an ambiguity.
Instead, either specify fully qualified names, like boost::function, boost::bind, or import particular symbols: using boost::function;.

Why is value only changed when referenced as calling argument?

In this code:
#include <iostream>
int num1 = 0;
using namespace std;
void add(int &number);
int main()
{
int num2;
int num3;
add(num1);
cout << "Num1 is: " << num1 << ". Yep, " << num1 << ".";
}
void add(int &number)
{
number++;
}
The passed value "num1" to "add" will change it, but in this code:
#include <iostream>
int num1 = 0;
using namespace std;
void add(int number);
int main()
{
int num2;
int num3;
add(num1);
cout << "Num1 is: " << num1 << ". Yep, " << num1 << ".";
}
void add(int number)
{
number++;
}
It does not change, the value of "num1". Why is that?
In the second case, you increment a COPY of the paramter. In the first case, with the "reference", you modify the variable itself.
Look up "pass by value vs pass by reference. For example:
Pass by reference
Call by value
It should be noted that C (unlike C++) is always "pass by value" ... but the equivalent can be achieved by passing a pointer to a variable.
In the first case number is a reference to num1. That is that number is reference to the same place in memory num1 refers to. Similar to a pointer but without the hassle of dereferencing it.
In the second number is created in the activation record of add, and placed on the stack. When add terminates the number variable is poped off (and the memory of number is deallocated) the stack, and num1 is left unchanged in main's stack and activation record.
If it were an object and not a primitive, this could be seen in the debugger by where the segfault would occur if you were to manually called the destructor just before add returned.

Resources