Cern ROOT 5.34 error message using functions - c++

I'm using the Cern ROOT version 5.34 and I have a problem using functions. The same message error always appear.
For example (using a very simple function):
*******************************************
* *
* W E L C O M E to R O O T *
* *
* Version 5.34/36 5 April 2016 *
* *
* You are welcome to visit our Web site *
* http://root.cern.ch *
* *
*******************************************
ROOT 5.34/36 (v5-34-36#v5-34-36, Apr 05 2016, 10:25:45 on win32)
CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] #include <iostream>
Note: File "iostream" already loaded
root [1] using namespace std;
root [2] void hello(){cout << "Hi!" << endl;}
Limitation: Function can not be defined in a command line or a tempfile
You need to write it in a source file (tmpfile)(1)
*** Interpreter error recovered ***
root [3]
How can I fix this?

If you used root 6, which supports all C++11 features, you could define a lambda function instead, like so
auto hello = [](){ cout << "Hi!" << endl; };
You can then call it just like a regular function
hello();

Related

`glog` example is not showing output as expected

I am trying to understand glog and therefore trying to run the example code on their github page.
I have installed glog (version - 0.6.0) and its dependency gflags (version - 2.2) on my mac OS (10.15.7)
I compile the example below
#include <glog/logging.h>
int main(int argc, char* argv[]) {
// Initialize Google’s logging library.
google::InitGoogleLogging(argv[0]);
// test with setting a value for num_cookies
int num_cookies = 3;
// ...
LOG(INFO) << "Found " << num_cookies << " cookies";
}
using the following command (and it compiles without any errors or warnings).
g++ glog-test.cpp -I/usr/local/include -L/usr/local/lib -lglog -lgflags -o glog-test.o
When I run the example using the following command,
./glog-test.o --logtostderr=1 --stderrthreshold=0
I expect to see the message Found 3 cookies on my terminal, but I see nothing being printed.
I have also experimented with different values for logtostderr (0, 1) and stderrthreshold (0, 1, 2, 3) and nothing gets written to the directory or gets printed on the terminal.
Any help in understanding what I am doing wrong here would be much appreciated, thank you!
You have to parse the command line flags manually through gflags::ParseCommandLineFlags
#include <glog/logging.h>
#incldue <gflags/gflags.h>
int main(int argc, char* argv[]) {
// Initialize Google’s logging library.
google::InitGoogleLogging(argv[0]);
gflags::ParseCommandLineFlags(&argc, &argv, true);
// test with setting a value for num_cookies
int num_cookies = 3;
// ...
LOG(INFO) << "Found " << num_cookies << " cookies";
}
Just checked, the following works..
GLOG_stderrthreshold=0 GLOG_logtostderr=1 ./glog-test.o
which gives
Found 3 cookies
I do have gflags and installed, as seen by
cd /usr/local/lib
ls -l | grep "libgflags"
which gives
-lrwxr-xr-x 1 sn admin 48 Jun 5 2020 libgflags.2.2.2.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags.2.2.2.dylib
lrwxr-xr-x 1 sn admin 46 Jun 5 2020 libgflags.2.2.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags.2.2.dylib
-rw-r--r-- 1 sn admin 170520 Jun 27 00:35 libgflags.a
lrwxr-xr-x 1 sn admin 42 Jun 5 2020 libgflags.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags.dylib
lrwxr-xr-x 1 sn admin 58 Jun 5 2020 libgflags_nothreads.2.2.2.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags_nothreads.2.2.2.dylib
lrwxr-xr-x 1 sn admin 56 Jun 5 2020 libgflags_nothreads.2.2.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags_nothreads.2.2.dylib
-rw-r--r-- 1 sn admin 168096 Jun 27 00:35 libgflags_nothreads.a
lrwxr-xr-x 1 sn admin 52 Jun 5 2020 libgflags_nothreads.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags_nothreads.dylib
so I am not sure why
./glog-test.o --logtostderr=1 --stderrthreshold=0
didn't work.

How to transfer constraints of string type to z3 solver expr in C++?

For example, I got a constraint: "(number < 10) && (name == "hello")";
I can do the following now:
context c;
expr number= c.int_const(number);
expr name = c->string_val(name.c_str());
expr constrain = ***procedure***("(number < 10) && (name == \"hello\")");
How can I implement this procedure()?
there is an incomplete and unproofed answer in Use a C++ string in z3::expr?, and I still can not figure out how to implement it?
I am very eagered and appreciateed for your help! Thanks!
Try:
#include <z3++.h>
using namespace z3;
using namespace std;
int main ()
{
context c;
expr number = c.int_const("number");
expr name = c.constant(c.str_symbol("name"), c.string_sort());
expr hello = c.string_val("hello");
expr constraint = number < 10 && name == hello;
solver s(c);
s.add(constraint);
cout << s.check() << "\n";
cout << s.get_model() << "\n";
return 0;
};
Assuming you put the above in a file called a.cpp, you compile it like this:
$ g++ -std=c++11 a.cpp -l z3
And when run, it produces:
sat
(define-fun number () Int
9)
(define-fun name () String
"hello")
Using higher-level APIs
As you no doubt noticed, programming z3 in C/C++ is very verbose and terribly error prone. Unless you've some other reason to use C/C++, I'd recommend using a higher-level API, such as Python or Haskell, which simplifies programming in z3 to a great extent.
Python
For instance, you'd code your problem in Python like this:
from z3 import *
number = Int('number')
name = String('name')
s = Solver()
s.add(number < 10, name == "hello")
print(s.check())
print(s.model())
producing:
sat
[number = 9, name = "hello"]
Haskell
And in Haskell, it would look like:
import Data.SBV
ex :: IO SatResult
ex = sat $ do number <- sInteger "number"
name <- sString "name"
constrain $ number .< 10 .&& name .== literal "hello"
producing:
*Main> ex
Satisfiable. Model:
number = 9 :: Integer
name = "hello" :: String
Summary
Long story short, programming z3 in C/C++—while entirely possible—is something that's best avoided if you can use a higher-level interface. If you must stick to C/C++, be sure to study the API: https://z3prover.github.io/api/html/namespacez3.html

Is there a way to make g++ compile this program with Unicode identifiers? [duplicate]

I am trying to use unicode variable names in g++.
It does not appear to work.
Does g++ not support unicode variable names, ... or is there some subset of unicode (from which I'm not testing in).
Thanks!
You have to specify the -fextended-identifiers flag when compiling, you also have to use \uXXXX or \uXXXXXXXX for unicode(atleast in gcc it's unicode)
Identifiers (variable/class names etc) in g++ can't be of utf-8/utf-16 or whatever encoding,
they have to be:
identifier:
nondigit
identifier nondigit
identifier digit
a nondigit is
nondigit: one of
universalcharactername
_ a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
and a universalcharactername is
universalcharactername:
\UXXXXXXXX
\uXXXX
Thus, if you save your source file as UTF-8, you cannot have a variable like e.g.:
int høyde = 10;
it had to be written like:
int h\u00F8yde = 10;
(which imo would beat the whole purpose - so just stick with a-z)
A one-line patch to the cpp preprocessor allows UTF-8 input. Details for gcc are given at
https://www.raspberrypi.org/forums/viewtopic.php?p=802657
however, since the preprocessor is shared, the same patch should work for g++ as well. In particular, the patch needed, as of gcc-5.2 is
diff -cNr gcc-5.2.0/libcpp/charset.c gcc-5.2.0-ejo/libcpp/charset.c
*** gcc-5.2.0/libcpp/charset.c Mon Jan 5 04:33:28 2015
--- gcc-5.2.0-ejo/libcpp/charset.c Wed Aug 12 14:34:23 2015
***************
*** 1711,1717 ****
struct _cpp_strbuf to;
unsigned char *buffer;
! input_cset = init_iconv_desc (pfile, SOURCE_CHARSET, input_charset);
if (input_cset.func == convert_no_conversion)
{
to.text = input;
--- 1711,1717 ----
struct _cpp_strbuf to;
unsigned char *buffer;
! input_cset = init_iconv_desc (pfile, "C99", input_charset);
if (input_cset.func == convert_no_conversion)
{
to.text = input;
Note that for the above patch to work, a recent version of iconv needs to be installed that supports C99 conversions. Type iconv --list to verify this, otherwise, you can install a new version of iconv along with gcc as described in the link above. Change the configure command to
$ ../gcc-5.2.0/configure -v --disable-multilib \
--with-libiconv-prefix=/usr/local/gcc-5.2 \
--prefix=/usr/local/gcc-5.2 \
--enable-languages="c,c++"
if you are building for x86 and want to include the c++ compiler as well.

correctly registering a plugin to use Eigen via Rcpp

I'm trying to be able to use a c++ class template in R. This was my first try at a small reproducible example.
library(inline)
library(Rcpp)
inc <-
"#include <Eigen/Dense>
template <size_t dim>
class SillyWrapper
{
public:
Eigen::Matrix<double,dim,1> m_vec;
SillyWrapper(const Eigen::Matrix<int,dim,1>& vec) : m_vec(vec);
};"
src <-
'SillyWrapper mything(Rcpp::as<Eigen::Map<Eigen::Matrix<double,dim,1>>>(x));'
library(inline)
fun <- cxxfunction(signature(x="numeric"),
body=src,
includes=inc,
plugin="Rcpp")
fun(rnorm(3))
How to I get access to the Eigen headers, though?
On my machine they are at /usr/include/eigen3/. I think I need to "register a plugin." I'm not sure where this file path goes in. I've tried a few of the named arguments, but no luck. Here's one example of something that I've tried that doesn't work:
library(inline)
library(Rcpp)
inc <-
'template <size_t dim>
class SillyWrapper
{
public:
Eigen::Matrix<double,dim,1> m_vec;
SillyWrapper(const Eigen::Matrix<int,dim,1>& vec) : m_vec(vec);
};'
src <-
'SillyWrapper mything(Rcpp::as<Eigen::Map<Eigen::Matrix<double,dim,1>>>(x));'
plug <- Rcpp.plugin.maker(include.before = "#include <Eigen/Dense>",
LinkingTo = "-I/usr/include/eigen3/") # correct arg name?
inline::registerPlugin("eigenDemo", plug)
fun <- cxxfunction(signature(x="numeric"),
body=src,
includes=inc,
plugin="eigenDemo")
I know there is an RcppEigen library that already exists, that would help with this example. If it's possible, I would like to see an answer that doesn't make use of this, though, because the information will be more applicable to other situations where this isn't available. Also, where do you put the c++11 flags? Apparently you can only use cxxfunction with one plugin at a time.
Edit:
Thanks #RalfStubner. For those interested, I have posted a follow-up question here.
Several comments:
I would use Rcpp::cppFunction instead of inline::cxxfunction.
As Dirk noted in the comments, why are you not using RcppEigen? In particular for Rcpp::as<Eigen::...> you will need that package anyway.
It is possible to create such a plugin on the fly, but setting include paths is a bit more complicated (see example below).
What are you trying to achieve? R won't know what to do with an instance of SillyWrapper.
Example, where I have used some code from the Eigen documentation due to point 4. above:
src <- '
void foo() {
Eigen::MatrixXd m(2,2);
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = -1;
m(1,1) = m(1,0) + m(0,1);
Rcpp::Rcout << "Here is the matrix m:\\n" << m << std::endl;
}'
Let's compile and run this code using the infrastructure provided by RcppEigen:
Rcpp::cppFunction(code = src,
depends = "RcppEigen",
includes = "#include <Eigen/Dense>")
foo()
#> Here is the matrix m:
#> 3 -1
#> 2.5 1.5
And now with our own plugin:
Rcpp::registerPlugin(
name = "Eigen3",
plugin = function(x) {
plug <- Rcpp::Rcpp.plugin.maker(include.before = "#include <Eigen/Dense>")
settings <- plug()
settings$env$PKG_CPPFLAGS = "-I/usr/include/eigen3"
settings
}
)
Rcpp::cppFunction(code = src,
plugins = "Eigen3")
foo()
#> Here is the matrix m:
#> 3 -1
#> 2.5 1.5
Created on 2019-09-04 by the reprex package (v0.3.0)
So while it is in principle possible to write an on-the-fly plugin for a system installed Eigen library, I am unsure how useful that would be given that RcppEigen exists.

Appcrash on running UmfPackLU<> with Eigen library

I am compiling and trying to run the UMfPackLU<SparseMatrix<>> routine from Eigen 3.2.9 and UMFPACK v4.5 libraries with TDM-GCC 5.1.0 on Win64 platform. But I am getting Appcrash with exception code c0000005.
What I need to implement is the following:
_ _ _ _
A = | P |, B = | R |, where P and Q are sparse and Z is 0 with 3 cols
| Q | | Z |
|_ _| |_ _|
X = A\B;
What I am doing (excerpt only) is the following:
#define num_t double
...
SparseMatrix<num_t,RowMajor> A(P.rows()+Q.rows(), P.cols());
A.topRows(P.rows()) = P;
A.bottomRows(Q.rows()) = Q;
Matrix<num_t, Dynamic, 3> B(P.rows()+Q.rows(), 3);
B.topLeftCorner(P.rows(), 3) = R;
B.bottomLeftCorner(Q.rows(), 3) = S;
UmfPackLU<SparseMatrix<num_t>> solver(A.transpose()*A);
auto AtB = A.transpose()*B;
X.col(0) = solver.solve(AtB.col(0)); // ### segmentation error here ###
X.col(1) = solver.solve(AtB.col(1));
X.col(2) = solver.solve(AtB.col(2));
Note the SparseMatrix<> is in RowMajor format.
On debugging with gdb: I get Program received signal SIGSEGV, Segmentation fault. at the line marked as above`.
Instead of UmfPackLU<SparseMatrix<>>, solving with SimplicialLLT<SparseMatrix<>>, SimplicialLDLT<SparseMatrix<>> or CholmodDecomposition<SparseMatrix<>> is working correctly.
Thanks in advance for any help.
This is a shortcoming in Eigen 3.2.9 that has been fixed a while ago in the 3.3 branch. It's now fixed in the 3.2 branch too (changeset 1e7d97fea51d).
You can workaround the issue by calling compute(...) instead of the constructor:
UmfPackLU<SparseMatrix<num_t>> solver;
solver.compute(A.transpose()*A);
Please feel free to correct/enhance/establish my answer.
What I have found is that, I need to instantiate an explicit ColMajor matrix AtA before feeding it to the solver (RowMajor is not working) as follows:
SparseMatrix<num_t, ColMajor> AtA = A.transpose()*A;
UmfPackLU<SparseMatrix<num_t>> solver(AtA);
Is this a requirement due to Eigen's lazy evaluation implementation for calling an external routine?