caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Side effect
@ 2002-08-08 22:40 Nicolas FRANCOIS
  2002-08-09  6:43 ` Florian Hars
  0 siblings, 1 reply; 4+ messages in thread
From: Nicolas FRANCOIS @ 2002-08-08 22:40 UTC (permalink / raw)
  To: Caml List

Thanks for the tip on stdout, I found the problem. Ocamldebug had me
discover another bug (a copy-paste bug ;-), but I'm stuck on this one :

  let cherche_pivot m fllig flcol =
    try
      for i = 0 to D.lignes - 1 do
	if fllig.(i)
	then
	  for j = 0 to D.colonnes - 1 do
	    if flcol.(j)
	    then
	      if A.(==) m.(i).(j) A.zero
	      then ()
	      else raise (Pivot_trouve (i,j))
	  done
      done;
      raise Plus_de_pivot
    with Pivot_trouve (i,j) -> (i,j)

  let applique_pivot_ligne m fllig flcol i j div =
    let m' = Array.copy m
    and x = m.(i).(j)
    in
      for i' = 0 to D.lignes - 1 do
	if fllig.(i')
	then
	  let y = m'.(i').(j) in
	    for j' = 0 to D.colonnes - 1 do
	      if flcol.(j')
	      then m'.(i').(j') <-
		A.(//) (A.(--)
			  (A.( ** ) x m'.(i').(j'))
			  (A.( ** ) y m'.(i).(j'))) div
	    done;
	    m'.(i').(j) <- A.zero;
      done;
      m'
	
  let pivot_ligne_sans_echange m =
    let m' = Array.copy m
    and fllig = Array.make D.lignes true
    and flcol = Array.make D.colonnes true
    and divisor = A.one
    in
    let rec plse_aux mat fl fc div =
      try
	let (i,j) = cherche_pivot mat fl fc
	in
	  fl.(i) <- false;
	  fc.(j) <- false;
	  let mat' = applique_pivot_ligne mat fl fc i j div in
	    plse_aux mat' fl fc mat'.(i).(j)
      with Plus_de_pivot -> mat
    in
      plse_aux m' fllig flcol A.one	
end

It's just a fraction free Gauss reduction without lines exchanges. It
works :

  let m = Matrice_Z.parse
    (Stream.of_string
       "[[ 3, 4, -2, 1, -2],[ 1, -1, 2, 2, 7],[ 4, -3, 4, -3, 2],[ -1, 1,
        6, -1, 1]]");;
  Matrice_Z.print m;;

prints

  (  3  4  -2  1  -2  )
  (  1  -1  2  2  7  )
  (  4  -3  4  -3  2  )
  (  -1  1  6  -1  1  )

  let m' = Pivot_Z.pivot_ligne_sans_echange m;;
  Matrice_Z.print m';;

gives the correct 

  (  3  4  -2  1  -2  )
  (  0  -7  8  5  23  )
  (  0  0  20  72  159  )
  (  0  0  0  -556  -1112  )

Trouble is
  
  Matrice_Z.print m';;

answers

  (  3  4  -2  1  -2  )
  (  0  -7  8  5  23  )
  (  0  0  20  72  159  )
  (  0  0  0  -556  -1112  )

So matrix m was modified. But I work on a copy of m in
Pivot_Z.pivot_ligne_sans_echange. What's the problem doc ?

\bye

-- 

                   Nicolas FRANCOIS
            http://nicolas.francois.free.fr
 A TRUE Klingon programmer does NOT comment his code
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Side effect
  2002-08-08 22:40 [Caml-list] Side effect Nicolas FRANCOIS
@ 2002-08-09  6:43 ` Florian Hars
  2002-08-09 11:27   ` Nicolas FRANCOIS
  0 siblings, 1 reply; 4+ messages in thread
From: Florian Hars @ 2002-08-09  6:43 UTC (permalink / raw)
  To: Nicolas FRANCOIS (AKA El Bofo); +Cc: Caml List

Nicolas FRANCOIS (AKA El Bofo) wrote:
> So matrix m was modified. But I work on a copy of m in
> Pivot_Z.pivot_ligne_sans_echange. What's the problem doc ?

Yes, m' is a copy of m, but the elements of m are arrays, so the copy m' 
  contains references to the same arrays that m contains, so m.(i).(j) 
is the same cell as m'.(i).(j). See:

         Objective Caml version 3.04

# let m = [|[|1;2|];[|3;4|]|];;
val m : int array array = [|[|1; 2|]; [|3; 4|]|]
# let m' = Array.copy m;;
val m' : int array array = [|[|1; 2|]; [|3; 4|]|]
# m'.(1).(1)<-99;;
- : unit = ()
# m';;
- : int array array = [|[|1; 2|]; [|3; 99|]|]
# m;;
- : int array array = [|[|1; 2|]; [|3; 99|]|]
# m'.(1) <- [|0;0|];;
- : unit = ()
# m'
   ;;
- : int array array = [|[|1; 2|]; [|0; 0|]|]
# m;;
- : int array array = [|[|1; 2|]; [|3; 99|]|]
#

Yours, Florian.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Side effect
  2002-08-09  6:43 ` Florian Hars
@ 2002-08-09 11:27   ` Nicolas FRANCOIS
  2002-08-09 12:08     ` Pierre Weis
  0 siblings, 1 reply; 4+ messages in thread
From: Nicolas FRANCOIS @ 2002-08-09 11:27 UTC (permalink / raw)
  To: Florian Hars, Caml List

Le Fri, 09 Aug 2002 08:43:15 +0200 Florian Hars <florian@hars.de> a écrit
:

> Nicolas FRANCOIS (AKA El Bofo) wrote:
> > So matrix m was modified. But I work on a copy of m in
> > Pivot_Z.pivot_ligne_sans_echange. What's the problem doc ?
> 
> Yes, m' is a copy of m, but the elements of m are arrays, so the copy m'
> 
>   contains references to the same arrays that m contains, so m.(i).(j) 
> is the same cell as m'.(i).(j). See:
> 
>          Objective Caml version 3.04
> 
> # let m = [|[|1;2|];[|3;4|]|];;
> val m : int array array = [|[|1; 2|]; [|3; 4|]|]
> # let m' = Array.copy m;;
> val m' : int array array = [|[|1; 2|]; [|3; 4|]|]
> # m'.(1).(1)<-99;;
> - : unit = ()
> # m';;
> - : int array array = [|[|1; 2|]; [|3; 99|]|]
> # m;;
> - : int array array = [|[|1; 2|]; [|3; 99|]|]
> # m'.(1) <- [|0;0|];;
> - : unit = ()
> # m'
>    ;;
> - : int array array = [|[|1; 2|]; [|0; 0|]|]
> # m;;
> - : int array array = [|[|1; 2|]; [|3; 99|]|]
> #
> 
> Yours, Florian.

Thanks, Florian. I guessed something like this, but could not find a clear
explanation in the docs (not that the docs are bad, it's just the reader
being unable to read it correctly).

I'll have to write a matrix copy function :-)

\bye

-- 

                   Nicolas FRANCOIS
            http://nicolas.francois.free.fr
 A TRUE Klingon programmer does NOT comment his code
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Side effect
  2002-08-09 11:27   ` Nicolas FRANCOIS
@ 2002-08-09 12:08     ` Pierre Weis
  0 siblings, 0 replies; 4+ messages in thread
From: Pierre Weis @ 2002-08-09 12:08 UTC (permalink / raw)
  To: Nicolas FRANCOIS; +Cc: florian, caml-list

Hi Nicolas,

[...]
> Thanks, Florian. I guessed something like this, but could not find a clear
> explanation in the docs (not that the docs are bad, it's just the reader
> being unable to read it correctly).

You should consider to have a look to the FAQ of the language, either
in french or in english:

http://pauillac.inria.fr/caml/FAQ/FAQ_DEBUTANT-eng.html
http://pauillac.inria.fr/caml/FAQ/FAQ_DEBUTANT-fra.html

There you will get a detailed explanation to answer the question:

* My array is modified, I don't know why ? 

which seems relevant to your problem.

> I'll have to write a matrix copy function :-)
> 
>                    Nicolas FRANCOIS

In particular, there is a discussion there about implementing
matrices in Caml, plus a copy matrix function :)

Read the FAQ !

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-08-09 12:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-08 22:40 [Caml-list] Side effect Nicolas FRANCOIS
2002-08-09  6:43 ` Florian Hars
2002-08-09 11:27   ` Nicolas FRANCOIS
2002-08-09 12:08     ` Pierre Weis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).