Last Friday, I had to submit a homework which required me to evaluate $\Pr(A > B)$ and $\Pr(A = B)$, where $A$ and $B$ were two independent Poisson random variables with parameters $\alpha$ and $\beta$ respectively.
I then started evaluating the sum.
Then I was stuck. I couldn’t compute this sum also.
I googled for a solution for hours, and after I saw equation (3.1) in a paper, I gave up finding exact solutions.1 As a supporter of free software, I avoided using M$ Ex*, and wrote a program in C++ to approximate the above probabitities by directly adding them term by term.
Assume that Poisson r.v. A and B are indepedent
Parameter for A: 1.6
Parameter for B: 1.4
Number of terms to be added (100 <= N <= 1000): 8
P(A > B) = 0.423023, P(A < B) = 0.335224, P(A = B) = 0.241691
A one-line method for writing the content of a function which returns the factorial of a number.
Evaluation of a function inside GDB
Keller, J. B. (1994). A characterization of the Poisson distribution and the probability of winning a game. The American Statistician, 48(4), 294–298. ↩
I saw an example for range-based for loop in a Stack Overflow question and I compiled it in my own computer.1 Then I got an error.
$ g++ temp1.cpp
temp1.cpp: In function ‘int main()’:
temp1.cpp:9:18: error: range-based ‘for’ loops are not allowed in C++98 mode
for (int i : bar) {
^
temp1.cpp: In function ‘void foo(int (&)[3])’:
temp1.cpp:16:18: error: range-based ‘for’ loops are not allowed in C++98 mode
for (int i : bar) {
^
I also received similar message after I typed the command for compiling the program with clang++.
I searched “gcc range based for loop are not allowed in” on Google,
and realised that I forgot to add the flag -std=c++11
.2 After
doing so, I could successfully compile the program using either GCC
or clang++.
I think that clang++ is better than gcc in this case because the error message is brighter than the code. The word “warning” is in purple, while the ‘^’ is in yellow. This helps users to see what’s wrong.
temp1.cpp:9:16: warning: range-based for loop is a C++11 extension
[-Wc++11-extensions]
for (int i : bar) {
^
temp1.cpp:16:16: warning: range-based for loop is a C++11 extension
[-Wc++11-extensions]
for (int i : bar) {
^
2 warnings generated.
c++ Range based for-loop on array passed to non-main function on Stack Overflow. ↩
Error: Range-based ‘for’ loops are not allowed in C++98 mode on Stack Overflow. ↩
A DISLIN installation guide on Ubuntu was written more than six years ago, so I am a bit dubious that if steps 2 and 3 are still needed for the installation using a DEB file.2 Therefore, I overlooked an important command and I regret this after wasting about two hours on Google.
As a result, I just downloaded the DEB file from the DISLIN official website and use the GUI tool “Ubuntu Software Centre” to install the library. Then I directly copied the code from the book and tried to compile the program .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Author: David Yevick
// Source: A Short Course in Computational Science and Engineering
#include <iostream>
#include <dislin.h></dislin.h></iostream></p>
<p>using namespace std;</p>
<p>int main(int argc, const char *argv[])
{
// Some code for defining variables
// Omitted to avoid copyright problems
// An example of using DISLIN methods
qplot(array1,array2,numOfPts);
}
However, the compiler gave me the message that I had used an “undefined reference to qplot”.
I googled for two hours and found nothing that I can understand. I even tried compiling the following sample code found in section 2.7 of the official DISLIN online manual.
1
2
3
4
5
6
7
8
9
10
11
12
// Author: Helmut Michels
// Source: DISLIN 10.4—A Data Plotting Library
#include <iostream>
#include "discpp.h"</iostream></p>
<p>main()
{
Dislin g;
g.disini ();
g.messag (“This is a test”, 100, 100);
g.disfin ();
}
I have no hope on published books because their authors would expect
their students to do something easy—Use Dev-C++ on M$ Win*, or
*nix GUI IDE like Eclipse. However, some geeks won’t be
satisfied because GUI buttons/objects hide the commands away from
users. As a result, they don’t know what tasks are invoked by
clicking the button/object. In other words, they don’t know what
they are actually doing. Therefore, they have the motivation to
find out the real command for the compilation of C++ programs that
#include "dislin.h"
.
Stack Overflow has a few or no questions on the compilation of C++ programs that use DISLIN. The most relevant one that I managed to find is question 19118465. I glanced at the answers quickly. They have some commands like
$ gfortran EX11_1.f90 -o progrname -ldislin -I/path/to/DISLIN/modules
Then I guessed that I need the -ldislin
flag as well. I can’t
truly figure out the usage of -l
, -L
and -I
flags by just
looking at the man page of g++
, so I just tried everything. I
searched for some new pages, and returned to some web pages that I’ve
already visited, and continued this unproductive process…
I googled the websites and I saw the post on Linux for Research
mentioned before. I looked at the command that begins with g++
and
tried to adapt it to my needs, but it simply failed.
I thought that the official README maybe useful, so I browsed the
contents in /usr/local/dislin/README
. Some websites that teach
users to install DISLIN from a gzip tarball contain code that
manipulate the environment variables like DISLIN
, PATH
and
LD_LIBRARY_PATH
. But in the README
, it’s clearly stated that I
don’t need to do so.
f) To make DISLIN available for general use, write the following com-
mands to your .profile or to /etc/profile
DISLIN=directory
export DISLIN
PATH=${PATH}:${DISLIN}/bin
LD_LIBRARY_PATH=$DISLIN:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
Note: The environment variable DISLIN is not necessary if DISLIN
is installed in the default directory '/usr/local/dislin'.
I think that I don’t need to do the above settings, but I am not sure about that. Luckily, the following part that immediately follows the above section gave me some hint on what I, as a newcomer to DISLIN, can do.
4.) Compiling, Linking and Running the Example Programs
Now you can compile, link and run the example programs in the
directory $DISLIN/examples with the commands:
clink -a exa_c (for gcc)
cpplink -a exa_cpp (for g++)
iclink -a exa_c (for Intel icc)
sclink -a exa_c (Sun Studio cc)
...
Thus, I really went to /usr/local/dislin/example
and compiled
exa_cpp.cpp
using cpplink
with the -a
flag. It works! No
complaints from g++
! It seemed that I could give cpplink
a try,
but it quickly turned out that my idea failed to work. g++
either
complained that dislin.h
was not found, or there’s “undefined
reference” to a DISLIN function.
After that, I returned to Google again, and was redirected to the
same post on Linux for Research again. I glanced through the same
command that started with g++
.
$ g++ test.cpp -lXt -ldislin -lm
This time, I could read the sentence below the above command.
Don't forget the `-lXt`
I finally realised that I had forgotten an important principle in programming: Don’t do too much at one time. At the very first stage, I need to get things done, though the approach is not elegant.
With the above principle in my mind, I could merely compile a C++ program that made use of DISLIN by making the following changes in both the source file and the command that I issued.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Author: David Yevick
// Source: A Short Course in Computational Science and Engineering
#include <iostream>
#include "/usr/local/dislin/dislin.h"</iostream></p>
<p>using namespace std;</p>
<p>int main(int argc, const char *argv[])
{
// Some code for defining variables
// Omitted to avoid copyright problems
// An example of using DISLIN methods
qplot(array1,array2,numOfPts);
}
In the terminal, I typed the following command.
$ g++ foo.cpp -lXt -ldislin -lm
It works! Critical readers will complain about the way that I
include dislin.h
. I’m not satisfied with this either. Another
obvious missing part for those who want to customise the name of the
output executable file is the -o
flag. I delibrately did this so
as to keep things simple and clear.
After trying the -l
, -L
and -I
flags of the command g++
, I
realised that the -I
flag will do.
However, when I tried to do the same task for another time, it surprisingly failed. Looking closely at the two versions, we can draw the following conclusion.
# `DISLIN' is a variable that represents the path of DISLIN.
# In the default case, it is `/usr/local/dislin'.
$ g++ -lXt -ldislin -lm -I$(DISLIN) foo.cpp -o foo.out # Wrong!
$ g++ foo.cpp -o foo.out -lXt -ldislin -lm -I$(DISLIN) # Correct!
I conclude this post with the fact that the flags -g -Wall
can be
put either before or after foo.cpp
.
Posted via UltraBlog.vim.
Yevick, D. (2012). A Short Course in Computational Science and Engineering: C++, Java and Octave Numerical Programming with Free Software Tools. Cambridge University Press. ↩
See Linux for Research for the post. ↩
.gov
, making it a reliable source
of information.
Having a good naming convention can greatly reduce the number of syntax errors in the source code and improve its readability.
Posted via UltraBlog.vim.
]]>The authors of the above posts are likely to work in the IT sector. For new comers of programming, they may like to start from zero. On which platform can they start from? I’d recommend popular *nix distributions (e.g. Debian-based/Fedora-based ones) due to their package managing tools like APT or YUM. For a M$ Vis* Stu* like code completion on Vim, clang_complete is the right answer, but it is often hard to install on M$ Win*, but the installation is incredibly easy on *nix.12
Many writers of posts on clang_complete installation stopped
writing further after completing the whole installation and
configuration of the plugin, and I haven’t seen any post that uses
“hello world” as an example. This is alright since many users of
Vim are experienced programmers who would like to demonstrate some
advanced features of the editor with the additional plugin (e.g.
jumping between ctags), and a simple hello.cpp
can’t be used for
that. Moreover, many teachers would teach programming on M$ Win*,
and even if a *nix desktop is used, the work is most likely done
using an IDE on GUI (Emacs is a text editor and IDE that runs on
command line.) … The concept of modal editing may be too hard for
newbies to accept it, and you need to get several commands right
before entering any text into a buffer. The steep learning curve
might frighten learners away from Vim… Some computer users
believe in the myth that Vim and Emacs are too old and nobody use
them.
Anyways, the mixed use of both the built-in omni-complete function of
Vim (invoked by <C-n>
or <C-p>
) and the popup menu of
clang_complete (invoked by <C-x><C-u>
) to write a “hello world”
program is a productive way of doing the job. However, due to the
limitations of human mind, we can remember very few unfamiliar things
at a time. This is not the case in storage devices. They can capture
every word that come out of our mind, though many physical means of
storage of information are subject to aging, especially those in the
past… Luckily, information technology allows words, and even fonts,
to be replicated efficiently and almost identically. This has brought
the human race a variety of new subjects, such as web *ng, and has
extended our old knowledge into many fields, such as typography… In
addition, the advancement of technology enables us to capture visible
images and copy it rapidly, and I’ve recorded my experience of “hello
world” using clang_complete with Vim on a series of screenshots.
In addition to adding Wu’s custom VIMRC configuration for clang_complete, I’ve also installed vim-snipmate and vim-snippets for more auto-complete mappings.3
Before actually beginning the steps for writing a “hello world”, we should know that we won’t learn anything from successes. It’s failures that help us reflect on the mistakes in our understand of theories. Therefore, I’ll deliberately find something wrong, and elaborate on that using some ideas. In other words, that’s “to blow water” in Cantonese.
#include<iostream>
The following “hello world” procedure contains some wrong steps.
inc<Tab>
.stdio.h
as default. The
file name of the included file is selected so that you can input
something else.cout
is used later, we need to #include<iostream>
.
First type io
to override the default input.<C-n>
or <C-p>
to get a completion list.iostream
(without ‘s’).Obviously, there’s a syntax error at the line #include<iostream>
.
Thinking about the auto-completion function again, one will notice
that the correct step is to press <Esc>
after step 1, and use ci<
to clear the code inside the <>
block, that’s a good try, but I
personally think that the steps can be more beautiful.
This is just a part of correct steps. I leave the remaining parts of codes of this simple program into later sections.
This time, after typing #
at the beginning of the line, press
<C-x><C-u>
to use the automatic completion list, and choose the #
include
option with the $`header'
surrounded by a <>
block,
not a double quote. Use <Alt-Tab>
to select the inner <>
block
and type io<C-n>
to use the built-in (not plugin) function to
complete the code. (The <Alt-Tab>
keystroke doesn’t work in GUI,
since it means changing the window on focus.)
Note: In this method, stdio.h
needs to be included. Otherwise, the
built-in insert completion popup menu containing iostream
will not
be shown.
using namespace std;
<C-n>
for word-by-word completion.Analysis: Finding an item from a menu requires coordination of our eyes and hands, while typing the code directly requires more memory. For a newbie, the former should be easier since he/she doesn’t waste time to recall his/her memory. For a geek, the later should be faster since the process has been repeated for many time. Luckily, being the second in the popup list, the problem of reaction time does not matter much if one looks at the list from the top to the bottom. Moreover, typing the code directly may lead to syntax errors. This problem can be overcome if one has the capability to read the error message from the compiler and to browse code quickly.
The remaining parts of this post doesn’t make use of the clang_complete plugin.
Just typing main<Tab>
will do. That’s from vim-snippets.
If you type cout<Tab>
, you’ll get std::cout<< <<std::endl;
, with
the cursor located in between the two whitespace characters in the
middle. Using a regular expression to delete the std::
in front of
cout
and endl
.
Modifying the c.snippet file maybe a bit troublesome, and may bring
strange consequences to the plugin. If you don’t have time to study
the plugin, you may not like to bother it. Manually decreasing the
indentation of the line return 0;
each time is not likely to be an
elegant solution for Vim Lovers. The actual way to fix the problem
is using the command gg=G
in the normal mode.4
For some newbies, it might be too much for them to digest if I discuss
more on the creation of a sample makefile, which automatically
executes more complicated compiling commands with a simple make
command, searching “makefile tutorial” on the web will do. If you
don’t like the clang compiler, just substitute clang++
with g++
.
According to Henri Poincaré, a French mathematician, things are beautiful if they are simple. Repetition of facts build up our understanding. I end this post with a short and simple command that can be repeatedly used to compile CPP source files with different file names. For more details about that, refer to my earlier post titled Fast Compilation and Execution of Source Code.
Posted via UltraBlog.vim.
在win/gvim中以DLL方式使用clang_complete by slimzhao in ChinaUnix. ↩
[VIM] using clang_complete plugin @ Ubuntu 12.04 by Yaun-Yi Chang in 第二十四個夏天後. ↩
A Vim plugin for navigating C++ with libclang by Kevin Wu Won in A Small Matter of Programming. ↩
Vi iMproved—Vim p.73 by Steve Oualline. ↩