Category Archives: Uncategorized

Compiling Giac in Windows (VS2008)

Finally, the day has come when I can proudly write that I have successfully compiled a computer algebra system in windows using visual studio and I actually was able to compile it as a dynamic library and link it with another C++ program that uses it. Maybe in other operating system with gcc compiler this is a trivial task, in windows with msvc this is for me far from trivial.

Here is a walkthrough. It would help if you understood the steps in compiling giac with gcc (either with mingwc or in a cygwin environment) in windows. You can look at my previous blog post to get an idea. If you just want to compile and not think of the technicalities (steps below), you may download a project archive here.

First I explain how to compile the project (not link)

  1. Make sure you make a project with the following c++ source files:
    alg_ext.cc
    cocoa.cc
    csturm.cc
    derive.cc
    desolve.cc
    ezgcd.cc
    first.cc
    gauss.cc
    gausspol.cc
    gen.cc
    global.cc
    help.cc
    identificateur.cc
    ifactor.cc
    index.cc
    input_lexer.cc (and input_lexer.ll)
    input_parser.cc
    intg.cc
    intgab.cc
    isom.cc
    lin.cc
    maple.cc
    mathml.cc
    misc.cc
    modfactor.cc
    modpoly.cc
    moyal.cc
    pari.cc
    permu.cc
    plot.cc
    plot3d.cc
    prog.cc
    quater.cc
    risch.cc
    rpn.cc
    series.cc
    solve.cc
    sparse.cc
    subst.cc
    sym2poly.cc
    symbolic.cc
    tex.cc
    threaded.cc
    ti89.cc
    tinymt32.cc
    unary.cc
    usual.cc
    vecteur.cc
    
  2. For the sake of completeness I provided a sample project file with the corresponding source codes. global.cc had to be patched because we use MPIR.

  3. Visual studio may not have stdint.h and some code may use it. I have included a stdint.h in the project archive that I am going to share. Otherwise, it is easy to add the necessary typedef from stdint.h. You can create your own stdint.h by creating the following file:

    typedef __int8 int8_t;
    typedef unsigned __int8 uint8_t;
    typedef __int32 int32_t;
    typedef unsigned __int32 uint32_t;
    typedef __int64 int64_t;
    typedef unsigned __int64 uint64_t;
    

    I also patched global.cc (also in the project archive I am sharing) because mpir already defines R_OK (which is defined in global.cc). See the next item for explanation. The patch just changes line ca. 172 that defines R_OK for the first time. To avoid conflict with mpir I changed this definition to

    #ifndef HAVE_LIBMPIR 
      int R_OK=4;
    #endif
    
  4. Make sure you have the following preprocessor definition set in the Project Properties->Configuration Properties->Preprocessor
    WIN32
    WINDOWS
    HAVE_CONFIG_H
    IN_GIAC
    GIAC_VECTOR
    _USE_MATH_DEFINES
    VISUALC (not __VISUALC__)
    _CRT_SECURE_NO_WARNINGS
    _SCL_SECURE_NO_WARNINGS
    _CRT_NON_CONFORMING_SWPRINTFS
    STATIC_BUILTIN_LEXER_FUNCTIONS
    NO_CLOCK (clock() is not standardly defined in msvc)
    HAVE_NO_CWD (getcwd is not standardly defined in msvc)
    MS_SMART (otherwise in gen.cc #include "../../../_windows/src/stdafx.h" would cause error, stdafx.h does not reside in this relative directory by default, without this we also get error in ifactor.cc when calling PREFETCH)
    NO_UNARY_FUNCTION_COMPOSE
    MS_SMART
    HAVE_LIBMPIR (needed for a patch in global.cc when using mpir, R_OK is already defined. See attached files)
    
  5. Make sure in config.h of the original giac source code, the following are NOT defined:
    HAVE_GETCWD (getcwd  is not standardly defined in msvc)
    HAVE_SYS_TIME_H
    HAVE_LIBPTHREAD (otherwise you get a lot of VLA errors in vecteur.cc)
    HAVE_PTHREAD_H (otherwise you get a lot of VLA errors in vecteur.cc)
    HAVE_READLINE_HISTORY_H (they are not standardly available in msvc)
    HAVE_READLINE_READLINE_H (they are not standardly available in msvc)
    

    Also uncomment

     #define HAVE_NO_CWD 1 //(getcwd  is not standardly defined in msvc) 
  6. In line ca. 1634 of vecteur.cc where the method rand_1 is defined, the code uses rand() which is ambiguous in VS2008 (error C2660). I took the liberty and changed rand() to std::rand() (I hope this is what the author of the code wanted).
  7. In line ca. 35 of rpn.cc, after #ifndef NSPIRE there is #include which is nonexistent in MSVC. Workaround:
    changed #ifndef NSPIRE to #if !defined(NSPIRE) && !defined(__VISUALC__)
  8. In the original static_lexer.h, for some reasons the unicode literals are not understood as variables (probably an msvc setting that I have not figured out how to set). At the moment the fast and dirty (since I don’t think I am going to use these specific giac commands) solution I could think of is to enclose the line with at_LINEAR? and the last two lines of the code of static_lexer.h (where unorthodox characters are used as “at” variables) between #ifndef __VISUALC__ and #endif. This patch of static_lexer.h is also in the archive I am sharing.
  9. In some compilers input_lexer.ll and input_lexer.cc will cause some problems when only std::log(10) is used (compiler cannot detect what type of overload of log this is). Therefore I replace std::log(10) with std::log(10.0)

After doing all these, you should not get any serious compile errors (maybe you get some that I have overlooked, but I am sure they are easy to resolve). The next hurdle is to link. I made a fix but I am not sure if this is the best fix. There are global unary functions that are supposed to be defined somewhere and called by static_extern.h. The linker initially complained about unresolved external symbols: at_ugamma, at_regroup, at_is_inside, at_igamma, at_IP and at_FP. Maybe Bernard can help me on this, but the only way I could find to link was just to enclose these external global initialization (which should be defined somewhere, but where?) within #ifndef __VISUALC__ and #endif. This I did in both static_lexer.h and static_extern.h

After doing this, I was able to finally link and compile. One thing that is not mentioning is that I used CMake to export dll as it is often required in Visual Studio to apply __declspec(dllexport) to the methods we want to export when compiling the dynamic library and apply __declspec(dllimport) when importing from the library. The new CMake (not so new anymore) allows you to do this (with additional parameters in Pre-Link Build Events of Visual Studio). I will not go into detail about this, this can be read here .
This allowed me to both created a giac dll and a lib. The end result is around 8.7Mb for both the dll and the lib, but since the dll is only shared I find this a significant improvement to the size you get from compiling with mingw32.

Finally, to test that it works. I linked giac to another project having only one source (make sure you add the include directories of giac, mpir and mpfir in C/C++ -> Additonal Include Directories of the project property):

#include <giac/config.h>
#include <giac/giac.h>

using namespace std;
using namespace giac;

gen pgcd(gen a,gen b){
  gen q,r;
  for (;b!=0;){
    r=irem(a,b,q);
    a=b;
    b=r;
  }
  return a;
}

int main(){
  cout << "Enter 2 integers ";
  gen a,b;
  cout << "Trying a giac self-implementation of gcd:\n";
  cin >> a >> b;
  cout << pgcd(a,b) << endl;
  return 0;
}

I was able to compile, link and execute this program and get the result similar to the one I got in my last blog (compiling in mingw32).

As promised here is a 7zip file containing the project and the giac source I compiled with (and the setup needed in VS2008). Note: I only set Release configuration and you still need to link/compile with mpir (you can get the source here) and set it up in the project for this to work.

Animating in Mathematica

M. gave us a lecture on how $S_4$ can be regarded as rigid transformation (rotations about the origin) of a cube centered at the origin. This was not straightforward for me to visualize. So ,after a long contemplation what to use: OpenGL/C++, Povray+Blender, Mathematica and its very useful animation features, I ended up deciding to use Mathematica. I had an intuition that Mathematica, albeit not being perfect in animation and visualization like Blender and Povray will provide for a very fast result (which at the moment was a priority for me). To visualize this you can use the following code:

g=Graphics3D[{Opacity[0.3], Cuboid[{-5, -5, -5}, {5, 5, 5}], 
  Opacity[1.0], Thick, Green, Line[{{-5, -5, -5}, {5, 5, 5}}], 
  Line[{{5, -5, -5}, {-5, 5, 5}}], Line[{{-5, 5, -5}, {5, -5, 5}}], 
  Line[{{-5, -5, 5}, {5, 5, -5}}], Black, 
  Text[Style[1, Large, Bold, Red], {5, 5, 5}],
  Text[Style[2, Large, Bold, Red], {5, -5, 5}],
  Text[Style[3, Large, Bold, Red], {-5, 5, 5}],
  Text[Style[4, Large, Bold, Red], {-5, -5, 5}]},Boxed->False,PlotRange->{{-9,9},{-9,9},{-9,9}}];
Animate[MapAt[Rotate[#, t,{0,5,5},{0,0,0}]&,g,{1}],{t,0,Pi},
  AnimationRunning->False,AnimationRepetitions->1,AnimationDirection->ForwardBackward,FrameLabel->"Transposition(1,3)"]
Animate[MapAt[Rotate[#,t,{5,5,5},{0,0,0}]&,g,{1}],{t,0,2Pi/3},
  AnimationRunning->False,AnimationRepetitions->1,AnimationDirection->ForwardBackward,FrameLabel->"3-Cycle (2,3,4)"]
Animate[MapAt[Rotate[#,t,{0,0,1},{0,0,0}]&,g,{1}],{t,0,Pi/2},
  AnimationRunning->False,AnimationRepetitions->1,AnimationDirection->ForwardBackward,FrameLabel->"4-Cycle (1,3,4,2)"]
Animate[MapAt[Rotate[#,t,{0,0,1},{0,0,0}]&,g,{1}],{t,0,Pi},
  AnimationRunning->False,AnimationRepetitions->1,AnimationDirection->ForwardBackward,FrameLabel->"2 Transpositions (1,4)(2,3)"]

Yes combining the Animate in one single panel was rather a pain as I did not know how to change variables (axis and angle of rotation) all in one panel (I wasn’t able to do it with Buttons and Dynamic in Mathematica).

If you do not have Mathematica, you can still download the animation file here. Now I am proud to claim that I did a Mathematica animation on my own: Time from 0-knowledge to one animation = Roughly 1 hour. If this was Blender+Povray, although I have experience in working with them, I would still need much more time to model, texture and finally generate the animation. So my conclusion is: although the animations and features of Mathematica has many things left to be desired, it is probably the best choice if you want to animate a few mathematical object very fast on the fly. The 1hr work I invested, is now a 5min. work for any other remainder animation I want to make in Mathematica. If the animation I want to do would require much more time than this, e.g. complicated interactions with buttons, phong and radiosity etc., I would probably look at other options as well.

To save an avi you may use something like the following (notice: I have hidden sliders and panel for a better look of the video).


Export["C:/temp/output1.avi",
Manipulate[MapAt[Rotate[#, t,{0,5,5},{0,0,0}]&,g,{1}],{t,0,Pi,
AnimationRunning->True,AnimationDirection->ForwardBackward,AnimationRepetitions->1,Paneled->False,ControlType->None},
FrameLabel->"Transposition(1,3)"]
]

This results in a very poor video compression (quality of videos were OK, but for 4sec. I got 35Mb of video). But that is easily remedied by using Handbrake (I do this if I don’t have too much time to play around more complicated tools) or VirtualDub (which I only use if I have a lot of time to invest.. which is sadly less often the case). I combined the four different videos showing a permutation from each conjugacy class of $S_4$. Here is the final video after compression (also a link was given earlier for you to download the video in case you do not have HTML5 support in your browser):

The Beginning of a Series of Math Blogs

I feel that I need to add more publicity to my mathematical works. Therefore, I have decided to add more mathematical discussion in my blogs. However, I will try to keep mathematical text in blogs to a minimum. I rather prefer a proper mathematical text in a pdf file than combinations of MathJaX, MathML etc. in a website. Though, I am quite aware that most people who browse would not really take the trouble to download or view a pdf file from the browser if they are not absolutely interested in the mathematical article. So what I will try to do in the future is to write a “preview” or abstract of what my paper or math article is about and maybe even write down the results in a blog and if people get interested they can freely download the full-fledged pdf article. Mathematical discussions in comments is naturally unavoidable, but for these I often just express my math by ascii.. Though sometimes, writing proper math equations and symbols becomes irresistible. And thus I installed the Simple MathJax plugin to help me write short descriptions of my mathematical work in a blog.

Beginning of a New Blog

This is a shoutout at Linz and its great service to the general public. I love this city. I really would like to stay here for the rest of my life if the city remains as it is. Many many thanks for their generosity for hosting my own personal website, email, blog and many other things! Thank you Linz. I will make you proud and I will be a proud Linzer. It starts with this Blog!