Help to convert matlab code to xojo

Hello,

I am new in xojo, i try to convert a small matlab code to xojo.
I can’t do it alone, any body in this forum for help ?

Please provide more details on what your code does (or needs to do). Does it use arbitrary precision, does it plot data, what types of functions does it use and so forth. Xojo as a language is not very fast at performing math intensive operations at this point though this will improve significantly with the incorporation of LLVM due by the end of the year. I am in the process of starting a small business which will focus on development of math software for Xojo and would like to understand your needs. But developing a set of code to emulate all the features of Matlab is a huge undertaking. Why do you want to move away from Matlab? Are you going to only migrate one piece of code?

The concept of small matlab code is not very significant
It could also be a single line of code that calls a matlab internal library extremely complex

@Djamel AIT AMRANE You might have a look at this site: http://www.dspguru.com/dsp/links/matlab-clones would properly be the right solution to use at the moment, as Xojo is very slow for this kind of math at this point.

I tried to code the project based on the genetic algorithms directly in Xojo, unfortunately it was impossible.
I have entrusted to my son, Control automation Engineer, for him it was easy to code it in matlab.
Now the program is running properly, the second step is to convert it into Xojo.
Right now, i am learning and translating all my Vb6 codes into Xojo.
Most of applications are mathematical,
celestial navigation, numerical methods of fix, perpetual almanac, geodetic computing , …

From my experience, given enough time and effort you can convert almost any kind of math software to Xojo. As stated above the converted code ,if pure Xojo, may be kinda slow. In my case I have developed helper functions in a ‘C’ library to accelerate my code in places. I would recommend starting with a single piece of code and post code segments that you are having trouble with so we can make recommendations. I don’t think you will find complete applications of the types you gave and will need to develop them.

The people on the forum here are very supportive but the more that you do on your own the better and the more specific the issue you have the better too. What works best is “I’m trying to do this specific thing in Xojo and here is my Xojo code segment, how can I make it work or improve it”

Hello James,
I have discovered a matrix plug in, by Dr. Robert M. Delaney, it’s very useful.
I have translated my old Vb6 maths functions, all what i need for numerical computing.
I have already translate some matlab code, i have build a Pseudo Inverse matrix for my least square fitting.
I am still trying to translate, i am proceeding by segment of code, some times i have break, could you help in this case ?
Because i am not familiar with matlab, my son does not know Xojo !

[quote=175175:@William James]From my experience, given enough time and effort you can convert almost any kind of math software to Xojo. As stated above the converted code ,if pure Xojo, may be kinda slow. In my case I have developed helper functions in a ‘C’ library to accelerate my code in places. I would recommend starting with a single piece of code and post code segments that you are having trouble with so we can make recommendations. I don’t think you will find complete applications of the types you gave and will need to develop them.

The people on the forum here are very supportive but the more that you do on your own the better and the more specific the issue you have the better too. What works best is “I’m trying to do this specific thing in Xojo and here is my Xojo code segment, how can I make it work or improve it”[/quote]

I do not know it that would help, but my tests of XojoScript have shown spectacular improvements on basic math.

Michel, for me XojoScript has too many limitations on how you can use it. I am betting when LLVM is available on all platforms that there will be much less use for XojoScript. From my experience, converting math intensive portions of code to C gives anywhere from 15 to 25x performance improvement. Xoxo’s staff estimates that LLVM will give about 10x improvement on math intensive code and agrees that C code (if implemented well) will always be faster than Xojo Code. My numbers are based on real world testing. My library is coded in pure Xojo and has a conditional compiler flag to replace/not replace portions with C code for the very intensive areas (e.g. FFT multiplies). I am looking forward to LLVM on the Mac to see how close the LLVM performance really does come to the C code.

Djamel, I would be glad to answer questions when you have them, time permitting. And I have collaborated with Bob Delaney in the past and he is very helpful as well.

James,
As i told you, i am not familiar with matlab, in VB6 it is possible to avoid the conversion, in the past i compile
matlab code linked to Vb6 by a COM Object using matlab Com builder and C/C++, it is very efficient in Vb6 considered
as so fast.
In Xojo, i can not do this.

James, thank you for your availability.

[quote=175316:@Djamel AIT AMRANE]James,
As i told you, i am not familiar with matlab, in VB6 it is possible to avoid the conversion, in the past i compile
matlab code linked to Vb6 by a COM Object using matlab Com builder and C/C++, it is very efficient in Vb6 considered
as so fast.
In Xojo, i can not do this.

James, thank you for your availability.[/quote]

Djamel, you can call COM objects from Xojo. See http://www.xojo.com/blog/en/2014/01/accessing-net-code-from-xojo.php

I have since done that with VB DLL.
https://forum.xojo.com/15785-accessing-net-code-from-xojo-c-custom-dll/0

Djamel,

I am not an expert in either Xojo or Matlab, but I use both of them. I can try to help. Can you post what you are trying to convert?

As a side note, I also have a project involving genetic algorithms. Right now it is coded in C# and I wanted to rewrite it in either matlab or Xojo (because I don’t know c# and I am not planning to learn an additional language). Matlab’s sintax is horrible (in my opinion) but it has so many functions built in…

Julen

Hi Julen - Is the C# code calling Matlab functions or are the functions written in C#?

[quote=175339:@Julen Ibarretxe Uriguen]Djamel,

I am not an expert in either Xojo or Matlab, but I use both of them. I can try to help. Can you post what you are trying to convert?

As a side note, I also have a project involving genetic algorithms. Right now it is coded in C# and I wanted to rewrite it in either matlab or Xojo (because I don’t know c# and I am not planning to learn an additional language). Matlab’s sintax is horrible (in my opinion) but it has so many functions built in…

Julen[/quote]

Hello Julen,

This is my matlab GA code ziped, five functions + main prog.

[url=http://www.partage-facile.com/SE6DY7S8OF/algo_gen_nav.rar.html[/url]

It’s only C# if I am not mistaken. It may use some library but I doubt it, and in any case it’s not calling any matlab generated library. The code is not mine, so I am not 100% sure.

If you are going to propose that I reuse part of it as a dll, I would prefer not to. The project is not very well coded, and I think it’s better to rewrite it from scratch.

Djamel,

I have checked the project and I think it would not take too long to convert it to Xojo. As I said, I can help, but I don’t have the time to do it for you. There isn’t anything that uses exotic built in functions in the code.

Begin with the main file (ga.m). There are a couple of matlab gotchas there (like matrix operations, sum, min) but if your son knows matlab he can explain what they do and you can easily code them in xojo.

If you need help with any specific part let me know.

Julen

Thank you Julen,
Yes i have contact with my son, he can gives me more information about the matlab implementation.

I have already covert a 5 functions but does not tested.

the main :

  Dim Trigo as New Maths.Trigo
  Dim Vecteur as New Maths.Vecteur
  Dim GeoNav as New Navigation.GeoNav
  Dim AlgoGen as New Maths.AlgoGen
  Dim r As New Random
  
  dim GHA(999),Dec(999),H0(999),Lat(999),Ljuste,LamJuste,L_Old,L_New,tol,pc,pm,pr,Lini, epsilon as double
  Dim NbObs,i as integer
  Dim  RandNum(999),lam(999) as double
  Dim Lamini,N as double
  Dim  zeros(9,9),Hc(9,9) as double
  Dim TheArray(999) as double
  
  // donnes d'obs.  input data
  
  H0(1)=(15+19.3/60)
  H0(2)=(77+34.9/60)
  GHA(1)=(131+24.8/60)
  GHA(2)=(3+14.2/60)
  Dec(1)=(45+58.4/60)
  Dec(2)=(49+25.7/60)
  NbObs = 2                                //number of obs.
  
  For i=1 to NbObs
      H0(i)= H0(i)*Trigo.Rad
    GHA(i)=GHA(i)*Trigo.Rad
     Dec(i)= Dec(i)*Trigo.rad
  Next i
  
  // rsultat a trouver: output data
  Ljuste=(41+39.1/60)*Trigo.Rad
  Lamjuste=(17+7.3/60)*Trigo.Rad
  
  //parametres initiaux
  tol=2*Trigo.Rad                 //limite des perturbations
  pc=0.5                                 //seuil du croisement
  pm=0.05                             //seuil de la mutation
  pr=1*Trigo.Rad                  // limite mutation
  Lini=42*Trigo.Rad             // longitude initiale
  Lamini=18*Trigo.Rad        // latitude initiale
  N=50                                    //nombre d'individus
  epsilon=1e-4                       //critere d'arret
  //randnum:    vecteur de N nombre alatoire entre -1 et 1  (la fonction rand renvoie un rel entre 0 et 1)
  
  Dim L(999) ,L0, Lam0, sel_m(),sel_s,L_mu,L_s,Lam_mu,Lam_old,Lam_s,RMSE(999),MinRMSE as double
  Dim j , k, g as integer
  
  // cration de N individus d'une maniere stochastique
    
  For i=1 To N
    L(i)=Lini+tol*(2*rnd-1)
    Lam(i)=Lamini+tol*(2*rnd-1)
    //RMSE(i) = Vecteur.Length(L(i))   Trouble with RMSE(i) 
  Next i
  

Hi again

There is one thing you should take into account when making the translation: matlab and xojo are very different. Whereas it is perfectly possible to write procedural code in xojo and matlab is said (by math works…) to be object oriented (OOP), Xojo helps/guides you towards OOP and matlab does not.

To me, the natural (xojo) approach here is that each indiviudual is an instance of the class Individual. That class will have properties (whatever the properties of the individual solutions of your problem are, I haven’t looked at the code into that detail) and methods (create, clone, mutate, …). Your population will be contained in an array of individuals. The evaluation function result (RMSE in this case?) can be a property too.

So what I am suggesting is that you don’t try to convert the code line by line, but understand it and rewrite it in xojo.

Julen

Hello Julen,

I try to understand, i think proceeding line by line is the best way for me, i am not able to think in xojo.
I will proceed to write procedural code, i am still so far to the OOP.
The priority is to replace all the zeros(1,n) and zeros(m,1) by Xojo vector notation, after that i can progress.
any idea ?

Zeros is a way of declaring an array with a fixed size. Matlab fills it with zeros (hence the name). You don’t need that in xojo, just use the regular Dim.