From: rif
Subject: My own max?
Date: 
Message-ID: <wj03cqf1s7b.fsf@five-percent-nation.mit.edu>
CL provides the build in max, which works on real scalars.  I want to
define a max that works on matlisp (http://matlisp.sourceforge.net)
matrices.  Obviously, I can define a function called matrix-max that
DWIW.  Is there any way to have it just be called max?  It doesn't
seem that I can redefine max as a generic function.  Is there some
other way?

rif

From: Paul F. Dietz
Subject: Re: My own max?
Date: 
Message-ID: <ldCdnfPLTLRLw1WgXTWc3g@dls.net>
rif wrote:

> CL provides the build in max, which works on real scalars.  I want to
> define a max that works on matlisp (http://matlisp.sourceforge.net)
> matrices.  Obviously, I can define a function called matrix-max that
> DWIW.  Is there any way to have it just be called max?  It doesn't
> seem that I can redefine max as a generic function.  Is there some
> other way?

You could work in a package in which MAX (which you define yourself)
is not CL:MAX.

	Paul
From: Carl Shapiro
Subject: Re: My own max?
Date: 
Message-ID: <ouyd6pja6qo.fsf@panix3.panix.com>
rif <···@mit.edu> writes:

> CL provides the build in max, which works on real scalars.  I want to
> define a max that works on matlisp (http://matlisp.sourceforge.net)
> matrices.  Obviously, I can define a function called matrix-max that
> DWIW.  Is there any way to have it just be called max?  It doesn't
> seem that I can redefine max as a generic function.  Is there some
> other way?

Yes, it's actually quite easy.  Create a package which shadows the
Common Lisp MAX symbol.

(defpackage my-matrix-lisp (:use common-lisp matlisp) (:shadow max))

(in-package my-matrix-lisp)

(defun max (thing &rest more-things) #| your code here |# ) 
From: Tim Bradshaw
Subject: Re: My own max?
Date: 
Message-ID: <ey3lm46byss.fsf@cley.com>
* Carl Shapiro wrote:

> Yes, it's actually quite easy.  Create a package which shadows the
> Common Lisp MAX symbol.

> (defpackage my-matrix-lisp (:use common-lisp matlisp) (:shadow max))

> (in-package my-matrix-lisp)

> (defun max (thing &rest more-things) #| your code here |# ) 

At the risk of blowing my own trumpet, my conduits system makes this
somewhat easier - in particular it helps you define packages which are
`like' another package but define a few (or many) symbols of their
own.  This makes the life of people who want to use these packages
much easier - you don't have to continually remember to shadow things.

    ;;;; Package definition file
    ;;;
 
   (in-package :org.tfeb.clc-user)

    (defpackage :cl/my-max
      ;; This is `like' CL but MAX is a different symbol - it's
      ;; a `conduit' package.
      (:use)
      (:extends/excluding :cl #:max)
      (:export #:max))

    (defpackage :cl/my-max-user
      ;; now user packages don't have to worry about shadowing &c
      (:use :cl/my-max))

    ;;;; define cl/my-max:max in some other file
    ;;;

    (in-package :cl/my-max-user)

    (defun max (...) 
      ...
      (cl:max ...))

Of course, the ORG.TFEB.CLC package (which is what ORG.TFEB.CLC-USER
users instead of CL) is the canonical example of one of these conduits
- it's just like CL except things like DEFPACKAGE are its own symbols.

You can define packages which are conduits for many packages, and I do
this really a lot: for instance I have things like:

    (defpackage :com.cley.weld.low
      ;; the Weld low-level interface
      (:use)
      (:extends :com.cley.weld.low.utils
                :com.cley.weld.low.io
                ...))

This is a significant benefit because it means that clients only need
to know about one package instead of 15 or something.

Conduits doesn't do anything you can't do anyway, it just provides a
convenient macro (ORG.TFEB.CLC:DEFPACKAGE).

Conduits can be found at http://www.tfeb.org/lisp/hax.html#CONDUITS.

--tim
From: Frode Vatvedt Fjeld
Subject: Re: My own max?
Date: 
Message-ID: <2hiszbft2z.fsf@vserver.cs.uit.no>
rif <···@mit.edu> writes:

> CL provides the build in max, which works on real scalars.  I want
> to define a max that works on matlisp
> (http://matlisp.sourceforge.net) matrices.  Obviously, I can define
> a function called matrix-max that DWIW.  Is there any way to have it
> just be called max?  It doesn't seem that I can redefine max as a
> generic function.  Is there some other way?

There is no decent way you can redefine cl:max (think what that would
do e.g. to other functions in the common-lisp package that uses
max). But you may define a package that provides all of the symbols of
the common-lisp package except cl:max, replacing it with your own
symbol. However, your best option is probably to name your function
matrix-max or something like that, unless you have some particular
reason why it must be named "max".

-- 
Frode Vatvedt Fjeld
From: Kaz Kylheku
Subject: Re: My own max?
Date: 
Message-ID: <cf333042.0211060914.c2abc15@posting.google.com>
rif <···@mit.edu> wrote in message news:<···············@five-percent-nation.mit.edu>...
> CL provides the build in max, which works on real scalars.  I want to
> define a max that works on matlisp (http://matlisp.sourceforge.net)
> matrices.  Obviously, I can define a function called matrix-max that
> DWIW.  Is there any way to have it just be called max? 

Yes. The notation ``max'' refers to the symbol X::max, where "X"
represents the name of the current package. So the trick is to define
your own package, intern the symbol "MAX" in that package, then use
that symbol as the name of your function.

Users of your package can import that symbol into their own package
such that it shadow's the COMMON-LISP::MAX.

  (defpackage :matlisp-extensions
    (:use :matlisp) ;; guessing about the name
    (:shadow :max :>) ;; matlisp probably uses "CL".
  )

  (in-package :matlisp-extensions)

  ;; We don't make max a generic function, but an ordinary
  ;; one which relies on a generic > function.

  (defgeneric > (left right))

  (defun max (&rest args)
    ;; iterate over args, pick largest with help of generic < func
    )

  (defmethod > ((left number) (right number))
    (cl:> left right))

  ;; other methods

How someone would use your package might be like this:

  (defpackage :my-application
    (:use :cl :matlisp)
    (:shadowing-import-from :matlisp-extensions :min :>))

  (in-package :my-application)

  ;; great, now MIN and > refer to the ones from MATLISP-EXTENSIONS

> It doesn't
> seem that I can redefine max as a generic function. 

Indeed, certainly not COMMON-LISP::MAX.
From: Kalle Olavi Niemitalo
Subject: Re: My own max?
Date: 
Message-ID: <878z065twi.fsf@Astalo.y2000.kon.iki.fi>
rif <···@mit.edu> writes:

> I want to define a max that works on matlisp
> (http://matlisp.sourceforge.net) matrices.

How do you define max on matrices, mathematically?
From: rif
Subject: Re: My own max?
Date: 
Message-ID: <wj0n0omux1x.fsf@five-percent-nation.mit.edu>
Kalle Olavi Niemitalo <···@iki.fi> writes:

> rif <···@mit.edu> writes:
> 
> > I want to define a max that works on matlisp
> > (http://matlisp.sourceforge.net) matrices.
> 
> How do you define max on matrices, mathematically?

The matrices have to be the same size, and then it's entrywise.
Alternately, you want the max of a matrix M and a scalar s to DTRT,
giving a new matrix M' satisfying M'_{ij} = max(M_{ij}, s).

rif