Re: Re: how to build pattern for a square matrix of reals?

*To*: mathgroup at smc.vnet.net*Subject*: [mg98539] Re: [mg98492] Re: how to build pattern for a square matrix of reals?*From*: DrMajorBob <btreat1 at austin.rr.com>*Date*: Sun, 12 Apr 2009 03:46:43 -0400 (EDT)*References*: <grhp95$mj7$1@smc.vnet.net> <grkgmm$6kq$1@smc.vnet.net>*Reply-to*: drmajorbob at bigfoot.com

You called l a "lower diagonal vector" and u an "upper diagonal vector" -- neither of which makes any sense. Only matrices, not vectors, can be upper or lower diagonal. I also noticed that you want 0 (an integer) to be treated as if it were Real. So here's a rendition of your "TridiagonalSolve": tridiagonal[___] := False tridiagonal[lower : {0 | 0., __Real}, d : {__Real}, upper : {__Real, 1 | 1.}, b : {__Real}] /; Equal @@ Length /@ {lower, d, upper, b} := True Notice I'd never, EVER use "l" as a variable name, as it's indistinguishable from "1" for many of us. I used the "lower" and "upper" names because you're thinking of them that way, even though vectors can't physically BE lower or upper. And here's a replacement for the second line of code, where I would NOT redefine the built-in Solve: realQ[0] = True; realQ[x_] := MatchQ[x, _Real] realQ[_] = False; commensurable[a_ /; MatrixQ[a, realQ], b : {__Real}] /; Union@Dimensions@a == {Length@b} := True commensurable[__] = False; Bobby On Sat, 11 Apr 2009 02:52:47 -0500, <sagrailo at gmail.com> wrote: > Once again: big thanks to all who replied! I think I'm able now to > fully get what I wanted. > > I guess it would be better if I explained initially what is all about: > I have some simple Fortran routines for solving tri-diagonal, and then > regular, system of linear equations, that I'm wrapping, through > Mathlink, for use from Mathematica. I want Mathematica to do arguments > checking for me, and this could be achieved through specifying > corresponding patterns for functions signature in the MathLink > template file. > > So, my functions are: > TridiagonalSolve[l, d, u, b] - for tridiagonal solver > Solve[A, b] - for regular solver > If the dimension of system is n in both cases, then here: > l - lower diagonal vector, of size n, with first element set to 0 > d - diagonal vector, of size n > u - upper diagonal vector, of size n, with last element set to 0 > b - right-side vector, of size n > A - matrix of the system, of size nxn > > So, I want to check that dimensions of all vectors match, for > tridiagonal solver function, and also that vector l has 0 as first, > and vector u has 0 as last element. Also, for regular solver > function, I want to check that dimensions of system matrix, and the > length of the right-side vector match. Of course, I also want to > check that A is matrix of reals, and that l, d,u and b are vectors of > reals. So here are final patterns I came up with (these seem to work > fine, but I'd appreciate any further suggestions on improvement): > > TridiagonalSolve[l_/;(VectorQ[l,MatchQ[#,_Real]&]&&l[[1]] > ==0),d_/;VectorQ[d,MatchQ[#,_Real]&],u_/;(VectorQ[u,MatchQ[#,_Real]&] > &&u[[-1]]==0),b_/;VectorQ[b,MatchQ[#,_Real]&]]/;Length[l]==Length[d] > ==Length[u]==Length[b]:="OK" > > Solve[A_/;(MatrixQ[A,MatchQ[#,_Real]&]&&Equal@@Dimensions > [A]),b_/;VectorQ[b,MatchQ[#,_Real]&]]/;Dimensions[A][[2]]==Length > [b]:="OK" > > Regards. > -- DrMajorBob at bigfoot.com