From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.129.159.211 with SMTP id w202mr9069985ywg.83.1479236224063; Tue, 15 Nov 2016 10:57:04 -0800 (PST) X-Received: by 10.157.20.197 with SMTP id r5mr1758114otr.9.1479236224009; Tue, 15 Nov 2016 10:57:04 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!1.eu.feeder.erje.net!feeder.erje.net!2.us.feeder.erje.net!newspeer1.nac.net!border2.nntp.dca1.giganews.com!nntp.giganews.com!p16no1507239qta.1!news-out.google.com!c26ni361itd.0!nntp.google.com!w132no95170ita.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 15 Nov 2016 10:57:03 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2601:191:8303:2100:302f:b9d8:b247:7dab; posting-account=fdRd8woAAADTIlxCu9FgvDrUK4wPzvy3 NNTP-Posting-Host: 2601:191:8303:2100:302f:b9d8:b247:7dab References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <3bd1d240-742b-4dc9-a733-6edf863c9d82@googlegroups.com> Subject: Re: matrix manipulation From: Robert Eachus Injection-Date: Tue, 15 Nov 2016 18:57:04 +0000 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Xref: news.eternal-september.org comp.lang.ada:32327 Date: 2016-11-15T10:57:03-08:00 List-Id: On Saturday, November 12, 2016 at 1:16:47 PM UTC-5, hn...@yahoo.de wrote: > What is an elegant way to program exchange of columns in a matrix.=20 > Application:=20 > C is a matrix. Cj be the j-th column of C. > Sort C1,C2, ..., Cn in non increasing order with respect to their first c= omponent. > I tried and ended up in a very messy program, as I had to sort the first = components first, remember their new index and then reconstruct the matrix. I don't consider the first part messy. I have lots of code lying around th= at creates an index of a set of records based on a greater than function wh= ich is passed in. Reusing the generic would require aliasing the array as = a matrix, easier just to change the spec and recompile--none of the generic= code depends on the contents of the record. The second part is more interesting. If you have a large array and even if= you don't it is more efficient to move columns to the correct location. Yo= u are going to need to copy one or more columns to a temporary location to = do the copying, but you can manage to copy most columns once. After you have the index array walk through it. If the index matches the c= urrent location go on. If it doesn't copy the row at the new location to t= he temporary vector (and keep track of where it goes), copy the current loc= ation to the emptied location, change its index to 0 or the right index (yo= ur choice) and start an inner loop. The inner loop recursively walks the indexes until it finds itself a zero (= or identity if you took that approach). Now unwind the recursion moving col= umns as you go. Are you finished? You can keep a count (remember to inclu= de columns not moved along with those moved to the temporary vector) or jus= t continue through the indexes looking for another column that needs moving= .