next up previous
Next: B. C 言語と行列 Up: C 言語のための matrix ライブラリィ Previous: 1.5 matrix ライブラリィを使用したプログラムの例

A. ソース

http://www.math.meiji.ac.jp/%7Emk/program/matrix/matrix-lib3.tar.gz
から入手できます。


/*
 * matrix.c
 */

#include "matrix.h"

matrix new_matrix(nrow, ncol)
int nrow, ncol;
{
    int i;
    vector ap;
    matrix a;

    if ((a = (matrix)malloc(nrow * sizeof(vector))) == NULL)
        return NULL;
    if ((ap = (vector)malloc(nrow * ncol * sizeof(scalar))) == NULL) {
        free(a);
        return NULL;
    }
    for (i = 0; i < nrow; i++)
        a[i] = ap + (i * ncol);
    return a;
}

void free_matrix(a)
matrix a;
{
    free(a[0]);
    free(a);
}


/*
 * vector.c
 */

#include "matrix.h"

vector new_vector(n)
int n;
{
    return (vector)malloc(sizeof(scalar) * n);
}

void free_vector(v)
vector v;
{
    free(v);
}


/*
 * dotprod.c
 */

#include "matrix.h"

double dotprod(n, u, v)
int n;
vector u, v;
{
    int i, n4;
    double s;

    s = 0;
    n4 = n & 3; /* n % 4 */
    for (i = 0; i < n4; i++)
        s += u[i] * v[i];
    for (i = n4; i < n; i += 4)
        s += u[i] * v[i] + u[i + 1] * v[i + 1]
            + u[i + 2] * v[i + 2] + u[i + 3] * v[i + 3];
    return s;
}


/*
 * matrix.h
 */

#ifndef MATUTIL
#define MATUTIL

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

#ifndef scalar
#define scalar double
#endif

typedef int    *ivector;
typedef float  *fvector;
typedef double *dvector;
typedef scalar *vector;

typedef ivector *imatrix;
typedef fvector *fmatrix;
typedef dvector *dmatrix;
typedef vector *matrix;

ivector new_ivector();
void free_ivector();

fvector new_fvector();
void free_fvector();

dvector new_dvector();
void free_dvector();

vector new_vector();
void free_vector();

imatrix new_imatrix();
void free_imatrix();

fmatrix new_fmatrix();
void free_fmatrix();

dmatrix new_dmatrix();
void free_dmatrix();

matrix new_matrix();
void free_matrix();

double dotprod();
#endif


#
# Makefile for matrix.a
#

SRCS    =       matrix.c vector.c dotprod.c
OBJS    =       matrix.o vector.o dotprod.o
LIB     =       matrix.a
PROGS   =       testmat test-glsc+ heat2d-e
CC      =       cc
CFLAGS  =       -O4
LIBDIR  =       /usr/local/lib
INCDIR  =       /usr/local/include
JUNK    =       Meta

default: $(LIB)
all: $(LIB) $(PROGS)

matrix.o: matrix.c
vector.o: vector.c
dotprod.o: dotprod.c
matrix.a: $(OBJS)
        ar cr $@ $(OBJS) && ranlib $@
clean:
        rm -f $(LIB) $(OBJS) $(PROGS) $(JUNK)
test: testmat
        testmat
testmat: testmat.c matrix.a
        $(CC) -o $@ $(CFLAGS) testmat.c matrix.a
test-glsc+: test-glsc+.c
        $(CC) -o $@ $(CFLAGS) test-glsc+.c -lglscd -L/usr/lib/X11 -lX11 -lm
heat2d-e: heat2d-e.c $(LIB)
        $(CC) -o $@ $(CFLAGS) heat2d-e.c $(LIB) -lglscd -L/usr/lib/X11 -lX11 -lm
install: matrix.a
        -mv -f $(LIBDIR)/libmatrix.a $(LIBDIR)/libmatrix.a.$$
        -mv -f $(INCDIR)/matrix.h $(INCDIR)/matrix.h.$$
        cp -p matrix.a $(LIBDIR)/libmatrix.a
        ranlib $(LIBDIR)/libmatrix.a
        cp -p matrix.h $(INCDIR)


next up previous
Next: B. C 言語と行列 Up: C 言語のための matrix ライブラリィ Previous: 1.5 matrix ライブラリィを使用したプログラムの例
Masashi Katsurada
平成18年4月29日