File: transpose.c

package info (click to toggle)
librsb 1.3.0.2%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 32,792 kB
  • sloc: ansic: 274,405; f90: 108,468; cpp: 16,934; sh: 6,761; makefile: 1,679; objc: 692; awk: 22; sed: 1
file content (157 lines) | stat: -rw-r--r-- 3,796 bytes parent folder | download | duplicates (3)
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
/*

Copyright (C) 2008-2021 Michele Martone

This file is part of librsb.

librsb is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

librsb is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public
License along with librsb; see the file COPYING.
If not, see <http://www.gnu.org/licenses/>.

*/
/*!
 @file
 @author Michele Martone

 @brief A toy <rsb.h>-based C program showing instantiation,
 transposition and other operations on a single matrix.
 Uses \ref rsb_mtx_clone(), \ref rsb_file_mtx_save(),
  \ref rsb_file_mtx_get_dims(), \ref rsb_file_mtx_load().

 \ingroup rsb_doc_examples

 \include transpose.c
*/
#include <rsb.h>
#include <stdio.h>	/* printf */

int main(const int argc, char * const argv[])
{
	struct rsb_mtx_t *mtxAp = NULL;
	const rsb_blk_idx_t brA = RSB_DEFAULT_BLOCKING,
                            bcA = RSB_DEFAULT_BLOCKING;
	rsb_nnz_idx_t nnzA = 4;
	rsb_coo_idx_t  nrA = 3;
	rsb_coo_idx_t  ncA = 3;
	const rsb_coo_idx_t    IA[] = { 0, 1, 2, 0 };
	const rsb_coo_idx_t    JA[] = { 0, 1, 2, 2 };
	const RSB_DEFAULT_TYPE VA[] = { 11, 22, 33, 13 };
	RSB_DEFAULT_TYPE XV[] = { 0,0,0,0,0,0 };
	rsb_coo_idx_t  vl = 0;
	const rsb_type_t typecode = RSB_NUMERICAL_TYPE_DEFAULT;
	rsb_err_t errval = RSB_ERR_NO_ERROR;

	/* library initialization */
	if(rsb_lib_init(RSB_NULL_INIT_OPTIONS)!=RSB_ERR_NO_ERROR)
	{
		return EXIT_FAILURE;
	}

	/* allocation */
	mtxAp = rsb_mtx_alloc_from_coo_const(
			VA,IA,JA,nnzA,typecode,nrA,ncA,
			brA,bcA,RSB_FLAG_NOFLAGS,NULL);
	if(!mtxAp)
	{
		return EXIT_FAILURE;
	}

	/* printout */
	if(RSB_ERR_NO_ERROR!=(errval = rsb_file_mtx_save(mtxAp,NULL)))
	{
		if(errval != RSB_ERR_UNSUPPORTED_FEATURE)
			goto err;
	}
	
	/* matrix transposition */
	if( RSB_ERR_NO_ERROR != (errval =
		rsb_mtx_clone(&mtxAp,RSB_NUMERICAL_TYPE_SAME_TYPE,
		RSB_TRANSPOSITION_T,NULL,mtxAp,RSB_FLAG_IDENTICAL_FLAGS)))
	{
		goto err;
	}

	/* printout */
	if(RSB_ERR_NO_ERROR!=(errval = rsb_file_mtx_save(mtxAp,NULL)))
	{
		if(errval != RSB_ERR_UNSUPPORTED_FEATURE)
			goto err;
	}

	rsb_mtx_free(mtxAp);

	/* doing the same after load from file */
	mtxAp = rsb_file_mtx_load("pd.mtx",
		RSB_FLAG_NOFLAGS,typecode,NULL);
	if(!mtxAp)
	{
		return EXIT_FAILURE;
	}

	/* printout */
	if(RSB_ERR_NO_ERROR!=(errval = rsb_file_mtx_save(mtxAp,NULL)))
	{
		if(errval != RSB_ERR_UNSUPPORTED_FEATURE)
			goto err;
	}

	/* one can see dimensions in advance, also */
	if(RSB_ERR_NO_ERROR!=(errval =
		rsb_file_mtx_get_dims("pd.mtx",&nrA,&ncA,&nnzA,NULL)))
	{
		if(errval != RSB_ERR_UNSUPPORTED_FEATURE)
			goto err;
	}

	/* A matrix can be rendered to Postscript. */
	{
		if(RSB_ERR_NO_ERROR!=(errval =
		rsb_mtx_rndr("pd.eps",mtxAp,512,512,RSB_MARF_EPS_B)))
			goto err;
	}

	rsb_mtx_free(mtxAp);

	/* also vectors can be loaded */
	if(RSB_ERR_NO_ERROR!=(errval = 
		rsb_file_vec_load("vf.mtx",typecode,NULL,&vl )))
		goto err;
	/* we expect vf.mtx to be 6 rows long */
	if( vl != 6 )
	{
		goto err;
	}

	if(RSB_ERR_NO_ERROR!=(errval = 
		rsb_file_vec_load("vf.mtx",typecode,XV, NULL )))
		goto err;

	/* matrices can be rendered from file to a pixelmap as well */
	{
		unsigned char pixmap[3*2*2];

		if(RSB_ERR_NO_ERROR!=(errval =
		rsb_file_mtx_rndr(pixmap,"pd.mtx",2,2,2,RSB_MARF_RGB)))
			goto err;
	}

	if(RSB_ERR_NO_ERROR != rsb_lib_exit(RSB_NULL_EXIT_OPTIONS))
	{
		goto err;
	}
	return EXIT_SUCCESS;
err:
	rsb_perror(NULL,errval);
	return EXIT_FAILURE;
}