File: ssfull_write.c

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 254,920 kB
  • sloc: ansic: 1,134,743; cpp: 46,133; makefile: 4,875; fortran: 2,087; java: 1,826; sh: 996; ruby: 725; python: 495; asm: 371; sed: 166; awk: 44
file content (168 lines) | stat: -rw-r--r-- 4,753 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* ========================================================================== */
/* === SuiteSparseCollection/ssfull_write.c ================================= */
/* ========================================================================== */

// SuiteSparseCollection, Copyright (c) 2006-2019, Timothy A Davis.
// All Rights Reserved.
// SPDX-License-Identifier: GPL-2.0+

/* SuiteSparseCollection: a MATLAB toolbox for managing the SuiteSparse Matrix
 * Collection.
 */

/* ========================================================================== */

/* ssfull_write (filename, X): write a full matrix to a file.  A small subset of
 * the Matrix Market format is used.  The first line is one of:
 *
 *	%%MatrixMarket matrix array real general
 *	%%MatrixMarket matrix array complex general
 * 
 * The 2nd line contains two numbers: m and n, where X is m-by-n.  The next
 * m*n lines contain the numerical values (one per line if real, two per line
 * if complex, containing the real and imaginary parts).  The values are
 * listed in column-major order.  The resulting file can be read by any
 * Matrix Market reader, or by ssfull_read.  No comments or blank lines are
 * used.
 */

#ifndef NLARGEFILE
#include "io64.h"
#endif

#include "mex.h"
#include <math.h>
#define MAXLINE 1030
#define BIG 1e308

/* -------------------------------------------------------------------------- */
/* print_value */
/* -------------------------------------------------------------------------- */

static void print_value (FILE *f, double x, char *s)
{
    double y ;
    int k, width ;

    /* change -inf to -BIG, and change +inf and nan to +BIG */
    if (x != x || x >= BIG)
    {
	x = BIG ;
    }
    else if (x <= -BIG)
    {
	x = -BIG ;
    }

    /* convert to int and back again */
    k = (int) x ;
    y = (double) k ;
    if (y == x)
    {
	/* x is a small integer */
	fprintf (f, "%d", k) ;
    }
    else
    {
	/* x is not an integer, use the smallest width possible */
	for (width = 6 ; width < 20 ; width++)
	{
	    /* write the value to a string, read it back in, and check */
	    sprintf (s, "%.*g", width, x) ;
	    sscanf (s, "%lg", &y) ;
	    if (x == y) break ;
	}
	fprintf (f, "%s", s) ;
    }
}


/* -------------------------------------------------------------------------- */
/* ssfull */
/* -------------------------------------------------------------------------- */

void mexFunction
(
    int nargout,
    mxArray *pargout [ ],
    int nargin,
    const mxArray *pargin [ ]
)
{
    int iscomplex ;
    mwSignedIndex nrow, ncol, i, j ;
    double *Ax, *Az ;
    char filename [MAXLINE], s [MAXLINE] ;
    FILE *f ;

    /* ---------------------------------------------------------------------- */
    /* check inputs */
    /* ---------------------------------------------------------------------- */

    if (nargout > 0 || nargin != 2)
    {
	mexErrMsgTxt ("usage: ssfull (filename,A)") ;
    }
    if (mxIsSparse (pargin [1]) || !mxIsClass (pargin [1], "double"))
    {
	mexErrMsgTxt ("A must be full and double") ;
    }

    /* ---------------------------------------------------------------------- */
    /* get filename and open the file */
    /* ---------------------------------------------------------------------- */

    if (!mxIsChar (pargin [0]))
    {
	mexErrMsgTxt ("first parameter must be a filename") ;
    }
    mxGetString (pargin [0], filename, MAXLINE) ;
    f = fopen (filename, "w") ;
    if (f == NULL)
    {
	mexErrMsgTxt ("error openning file") ;
    }

    /* ---------------------------------------------------------------------- */
    /* get the matrix */
    /* ---------------------------------------------------------------------- */

    iscomplex = mxIsComplex (pargin [1]) ;
    nrow = mxGetM (pargin [1]) ;
    ncol = mxGetN (pargin [1]) ;
    Ax = mxGetPr (pargin [1]) ;
    Az = mxGetPi (pargin [1]) ;

    /* ---------------------------------------------------------------------- */
    /* write the matrix */
    /* ---------------------------------------------------------------------- */

    if (iscomplex)
    {
	fprintf (f, "%%%%MatrixMarket matrix array complex general\n") ;
    }
    else
    {
	fprintf (f, "%%%%MatrixMarket matrix array real general\n") ;
    }
    fprintf (f, "%.0f %.0f\n", (double) nrow, (double) ncol) ;
    for (j = 0 ; j < ncol ; j++)
    {
	for (i = 0 ; i < nrow ; i++)
	{
	    print_value (f, Ax [i + j*nrow], s) ;
	    if (iscomplex)
	    {
		fprintf (f, " ") ;
		print_value (f, Az [i + j*nrow], s) ;
	    }
	    fprintf (f, "\n") ;
	}
    }

    /* ---------------------------------------------------------------------- */
    /* close the file */
    /* ---------------------------------------------------------------------- */

    fclose (f) ;
}