ラベル C言語 の投稿を表示しています。 すべての投稿を表示
ラベル C言語 の投稿を表示しています。 すべての投稿を表示

2015年8月2日日曜日

Gnuplot連携ライブラリ(C言語)

大学時代から数値計算結果のプロットは、もっぱらGnuplot(http://www.gnuplot.info/)を使っています。なんだかんだお手軽で便利なんですよねGnuplot。

C/C++から呼び出すときには、popen(3)でGnuplotを呼び出して要件に合わせてプロットするコードを書きますが、毎度毎度書くのは面倒で、いつかはライブラリ化しようと思いつつも、大学時代に自分用に作ったプロット用関数群を使用し続けていました。

最近久しぶりにコード書いてて、コードからGnuplot呼び出そうとしたときに、せっかくだからライブラリ化しよう思いました。大学時代の自分なら闇雲にいきなりコードを書いてしまったでしょうが(車輪の再発明が大好きだったのです)、当時よりは少しだけ冷静に行動できるようになっていますので、既に公開されているライブラリはないか探してみました。そして探してみたら、あっさり見つかりました。

gnuplot interfaces in ANSI Chttp://ndevilla.free.fr/gnuplot/

まだ、サンプルコードいじった程度ですが、シンプルで使いやすそう。自分で必要な機能を追加するにしても、これをベースに作っていこうかなと思いました。

/*
 * Examples of gnuplot_i.c usage
 */

#include <stdio.h>
#include <stdlib.h>

#include "gnuplot_i.h"

#define SLEEP_LGTH  2
#define NPOINTS     50

int main(int argc, char *argv[]) 
{
    gnuplot_ctrl    *   h1,
                    *   h2,
                    *   h3,
                    *   h4 ;
    double              x[NPOINTS] ;
    double              y[NPOINTS] ;
    int                 i ;

    /*
     * Initialize the gnuplot handle
     */
    printf("*** example of gnuplot control through C ***\n") ;
    h1 = gnuplot_init() ;

    /*
     * Slopes
     */
    gnuplot_setstyle(h1, "lines") ;
    
    printf("*** plotting slopes\n") ;
    printf("y = x\n") ;
    gnuplot_plot_slope(h1, 1.0, 0.0, "unity slope") ;
    sleep(SLEEP_LGTH) ;

    printf("y = 2*x\n") ;
    gnuplot_plot_slope(h1, 2.0, 0.0, "y=2x") ;
    sleep(SLEEP_LGTH) ;

    printf("y = -x\n") ;
    gnuplot_plot_slope(h1, -1.0, 0.0, "y=-x") ;
    sleep(SLEEP_LGTH) ;
    

    /*
     * Equations
     */

    gnuplot_resetplot(h1) ;
    printf("\n\n") ;
    printf("*** various equations\n") ;
    printf("y = sin(x)\n") ;
    gnuplot_plot_equation(h1, "sin(x)", "sine") ;
    sleep(SLEEP_LGTH) ;

    printf("y = log(x)\n") ;
    gnuplot_plot_equation(h1, "log(x)", "logarithm") ;
    sleep(SLEEP_LGTH) ;

    printf("y = sin(x)*cos(2*x)\n") ;
    gnuplot_plot_equation(h1, "sin(x)*cos(2*x)", "sine product") ;
    sleep(SLEEP_LGTH) ;


    /*
     * Styles
     */

    gnuplot_resetplot(h1) ;
    printf("\n\n") ;
    printf("*** showing styles\n") ;

    printf("sine in points\n") ;
    gnuplot_setstyle(h1, "points") ;
    gnuplot_plot_equation(h1, "sin(x)", "sine") ;
    sleep(SLEEP_LGTH) ;
    
    printf("sine in impulses\n") ;
    gnuplot_setstyle(h1, "impulses") ;
    gnuplot_plot_equation(h1, "sin(x)", "sine") ;
    sleep(SLEEP_LGTH) ;
    
    printf("sine in steps\n") ;
    gnuplot_setstyle(h1, "steps") ;
    gnuplot_plot_equation(h1, "sin(x)", "sine") ;
    sleep(SLEEP_LGTH) ;

    /*
     * User defined 1d and 2d point sets
     */

    gnuplot_resetplot(h1) ;
    gnuplot_setstyle(h1, "impulses") ;
    printf("\n\n") ;
    printf("*** user-defined lists of doubles\n") ;
    for (i=0 ; i<NPOINTS ; i++) {
        x[i] = (double)i*i ;
    }
    gnuplot_plot_x(h1, x, NPOINTS, "user-defined doubles") ;
    sleep(SLEEP_LGTH) ;

 printf("*** user-defined lists of points\n");
    for (i=0 ; i<NPOINTS ; i++) {
        x[i] = (double)i ;
        y[i] = (double)i * (double)i ;
    }
    gnuplot_resetplot(h1) ;
    gnuplot_setstyle(h1, "points") ;
    gnuplot_plot_xy(h1, x, y, NPOINTS, "user-defined points") ;
    sleep(SLEEP_LGTH) ;


    /*
     * Multiple output screens
     */

    printf("\n\n") ;
    printf("*** multiple output windows\n") ;
    gnuplot_resetplot(h1) ;
    gnuplot_setstyle(h1, "lines") ;
    h2 = gnuplot_init() ;
    gnuplot_setstyle(h2, "lines") ;
    h3 = gnuplot_init() ;
    gnuplot_setstyle(h3, "lines") ;
    h4 = gnuplot_init() ;
    gnuplot_setstyle(h4, "lines") ;

    printf("window 1: sin(x)\n") ;
    gnuplot_plot_equation(h1, "sin(x)", "sin(x)") ;
    sleep(SLEEP_LGTH) ;
    printf("window 2: x*sin(x)\n") ;
    gnuplot_plot_equation(h2, "x*sin(x)", "x*sin(x)") ;
    sleep(SLEEP_LGTH) ;
    printf("window 3: log(x)/x\n") ;
    gnuplot_plot_equation(h3, "log(x)/x", "log(x)/x");
    sleep(SLEEP_LGTH) ;
    printf("window 4: sin(x)/x\n") ;
    gnuplot_plot_equation(h4, "sin(x)/x", "sin(x)/x") ;
    sleep(SLEEP_LGTH) ;
    
    /*
     * close gnuplot handles
     */


    printf("\n\n") ;
    printf("*** end of gnuplot example\n") ;
    gnuplot_close(h1) ;
    gnuplot_close(h2) ;
    gnuplot_close(h3) ;
    gnuplot_close(h4) ;
    return 0 ;
}

2013年9月22日日曜日

C言語からTcl/Tkを呼び出す

前回のTcl/Tk版 Hello WorldをC言語から実行させることができたのでメモ.

まずは, ソース. Tcl_Initの前にTcl_FindExecutableが必須と説明している文書もあったが, 無くても動いたのでそのまま.
#include <tcl.h>
#include <tk.h>

int main(int argc, char* argv[])
{
    Tcl_Interp* interp;
    char        wish_cmd[] =
      "wm title . \"Hello World\"\n"
      "wm geometry . \"480x240\"\n"
      "label .label -text \"Hello, World! with Tcl/Tk.\"\n"
      "pack .label -side top -anchor w";
    
    interp = Tcl_CreateInterp();
    Tcl_Init(interp);
    Tk_Init(interp);
    Tcl_Eval(interp, wish_cmd);
    Tk_MainLoop();
    
    return 0;
}


次にコンパイル.
$ gcc -Wall hello_tcltk.c -ltcl86 -ltk86
/mingw/libの下にlibtcl86.aとlibtk86.aが配置されていたので, 上記のようにした.
また, CentOSでもTcl/Tkをインストールして, 上記のソースコードをコンパイルしてみた. こちらは, 以下のコマンドでOKだった.
$ gcc -Wall hello_tcltk.c -ltcl -ltk
ちなみに, これをMinGW/msysで実行すると, 当然のことながらld先生に「cannot find ...」と怒られる.