ATS2: printing a matrix -> do I need to write a praxi?

To print the matrix in its current form, do I need to create a praxi to get
the correct viewtype; is this the way to go?
If so I will try it - just wanted to make sure this is the way to go
forward.

staload
UN = “prelude/SATS/unsafe.sats”

(* ****** ****** *)

staload "libc/SATS/stdio.sats"
staload RG = “atshwxi/testing/SATS/randgen.sats”

(* ****** ****** *)

staload
INT = "prelude/DATS/integer.dats"
staload
FLOAT = “prelude/DATS/float.dats”

(* ****** ****** *)

staload “prelude/SATS/matrix.sats”

(* ****** ****** *)

staload _ = "prelude/DATS/gorder.dats"
staload _ = “prelude/DATS/gnumber.dats”

(* ****** ****** *)

(* ****** TODO ****** )
(

** Add scalar matrix multiplication
** make a matrix “addback” praxi?
*)

typedef T = double

implement
main0 (argc, argv) =
{

var out: FILEref = stdout_ref
val A = (arrayptr)$arrpsz{T}(0.0, 1.0, 2.0, 3.0)
//val pA = addr@(A)
val (pfarr | p) = arrayptr_takeout_viewptr (A)
prval pfmat = array2matrix_v {T}{…}{2,2}(pfarr)
// what does {…} do ^

val () = fprint_matrix {2,2} (out,
pfmat @ p, // <- I know this isn’t right, but not sure how to get what
we need.
i2sz(2), i2sz(2))

// Need to convert back to array or somehow free matrix here
prval pfarr = matrix2array_v(pfmat)
prval () = arrayptr_addback (pfarr | A)
val () = arrayptr_free (A)

} (* end of [main0] *)

OK, that seems right in this context.

Regarding the use of fprint_matrix, it seemed a castfn might prove useful,
but I get some errors with this, so it is not quite right:

extern castfn

matrix_v_encode :

{a:vt0p}{l:addr}{m,n:int}

(array_v (INV(a), l, m*n) | ptr l) -<0> matrix_v (a, l, m, n)

// end of [matrix_v_encode]

Also I switched strategies to attempt to use arrayptr2matrixptr_int:

staload "prelude/SATS/matrix.sats"
staload "prelude/SATS/array.sats"
staload "prelude/SATS/matrixptr.sats"
staload “prelude/SATS/arrayptr.sats”

val A = (arrayptr)$arrpsz{T}(0.0, 1.0, 2.0, 3.0)
val M = arrayptr2matrixptr_int{T}{…}{2,2}(A,2,2)

But I get::
matrix_test_dats.c: In function ‘mainats_argc_argv_0’:
matrix_test_dats.c:173:1: error: ‘PMVtmpltcstmat’ undeclared (first use in
this function)
matrix_test_dats.c:173:1: note: each undeclared identifier is reported only
once for each function it appears in
matrix_test_dats.c:173:1: error: ‘arrayptr2matrixptr_int’ undeclared (first
use in this function)
matrix_test_dats.c:173:1: error: expected expression before ‘>’ token

(What to put here?) : !p

… I am sure I had tried that last night–but now it works. Probably I
was doing something else wrong elsewhere.

I think the other problem related to PMVtmpltcstmat was that I needed to
remove some static includes, for some reason:

//staload “prelude/SATS/matrix.sats”

//staload “prelude/SATS/array.sats”

//staload “prelude/SATS/matrixptr.sats”

//staload “prelude/SATS/arrayptr.sats”

If I understand you correctly, you basically want the two
separators (item-separator and row-separator) to be the same.
This can be done as follows:

implement{}
fprint_matrix$sep2 (out) = fprint_matrix$sep1<> (out)

If you haven’t, please take a look at the following file:

matrix_encode should be a proof function; it is already available:

http://www.ats-lang.org/LIBRARY/prelude/matrix.html#matrix2array_v

‘PMVtmpltcstmat …’ means that the template definition of some function is
not available.

Try to add the following lines at the top:

(* ****** ****** )
//
#include
"share/atspre_staload_tmpdef.hats"
//
(
****** ****** *)

Thanks for the suggestions; I actually had just tried that, but still there
is the same issue with PMVtmpltcstmat. I will post the Makefile and code
later if I continue to purse the matrixptr route (I also need to start
using CMake ASAP).

I had originally been trying to use matrix.sats and I missed the matrixptr
example (which I’m unable to build at the moment, but I will try the new
patscc release soon). The problem in this (no matrixptr) context was that I
was not sure how to go from an array to a matrix and then be able to print
it. I tried, but I am most likely forgetting how to view ‘p’ as a matrix_v:

val (pfarr | p) = arrayptr_takeout_viewptr (A)
prval pfmat = array2matrix_v {T}{…}{2,2}(pfarr)
val () = fprint_matrix {2,2} (out, (What to put here?), i2sz(2),
i2sz(2))

If I recall correctly the {…} in “prval pfmat = array2matrix_v
{T}{…}{2,2}(pfarr)” is used to mark a position as to be inferred. Normally
the positions are inferred if they are left out but if it’s a place in the
middle of a number of arguments, as it is in this case, you need to mark it
in some way, hence the {…}.

http://www.bluishcoder.co.nz

It is unnecessary to re-staload these files (as there are automatically
loaded by the ATS2 compiler).
However, it should not be a mistake to re-staload them. Maybe it was caused
by a bug I have now fixed
as I don’t encounter the problem on my side.