From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,c1bdceb867926fdb X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!newsfeed.straub-nv.de!open-news-network.org!noris.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Interfacing Ada with C Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <0ee9eec7-6024-4fb8-8df0-f65c146e4b84@i28g2000yqa.googlegroups.com> <143ef70b-7e74-426b-a621-a5fd157849be@x21g2000yqa.googlegroups.com> Date: Sun, 25 Jul 2010 15:50:47 +0200 Message-ID: <18zszx6sjlloa$.k5nohxp9k27i$.dlg@40tude.net> NNTP-Posting-Date: 25 Jul 2010 15:50:48 CEST NNTP-Posting-Host: 8d74a882.newsspool1.arcor-online.net X-Trace: DXC=kYbCdXKDUI6]E=H1Q9`787ic==]BZ:af>4Fo<]lROoR1<`=YMgDjhg2ZFm4oJHSm]:[6LHn;2LCV>[51 X-Complaints-To: usenet-abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:12542 Date: 2010-07-25T15:50:48+02:00 List-Id: On Sun, 25 Jul 2010 05:21:44 -0700 (PDT), Ada novice wrote: > What would the best way to interface this with Ada? The elements of my > matrix will be formed in Ada, then the matrix will be passed to C to > calculate the eigenvalues and then the latter passed back to Ada. The > size of my matrix will be fixed say 3 x 3. As a novice in Ada, I would > like to learn good programming practice and the best way is to use > codes and learn from experts here in Ada. Something like (not tested): with Interfaces.C; use Interfaces.C; package IMSL is type Float_Matrix is -- Row-wise, packed/aligned array (Positive range <>, Positive range <>) of C_Float; pragma Convention (C, Float_Matrix); type F_Complex is record Re : C_Float; Im : C_Float; end record; pragma Convention (C, F_Complex); type Complex_Array is array (Positive range <>) of F_Complex; pragma Convention (C, Complex_Array); function F_Eig_Gen (Matrix : Float_Matrix) return Complex_Array; end IMSL; package body IMSL is procedure Free (Ptr : System.Address); pragma Import (C, Free, "free"); function F_Eig_Gen (Matrix : Float_Matrix) return Complex_Array is begin if Matrix'Length (1) /= Matrix'Length (2) then raise Constraint_Error; end if; declare -- No idea, how they report convergence/accuracy errors function Internal (N : Int; A : Address; Terminator : Address := Null_Address) return Address; pragma Import (C, Internal, "imsl_f_eig_gen"); Ptr : Address := Internal ( Matrix'Length (1), Matrix ( Matrix'First (1), Matrix'First (2) )' Address ); Data : Complex_Array (1..Matrix'Length (1)); for Data'Address use Ptr; pragma Import (Ada, Data); Result : Complex_Array := Data; -- Copy, we don't own that memory begin Free (Ptr); return Result; end; end F_Eig_Gen; end IMSL; Used as: Values : Complex_Array := F_Eig_Gen ( ( ( 8.0,-1.0,-5.0), (-4.0, 4.0,-2.0), (18.0,-5.0,-7.0) ) ); -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de