Matrix inversion

  • Thread starter Sprechen sie von C++
  • Start date
S

Sprechen sie von C++

I was looking to massage some code, wanted to add matrix inversion to my
matrix template and related functions.
I have had some issues trying to figure out math books and get it working so
I thought I would inquire here.
I have a vector template and a matrix template now
They support all manor of math operations
Ideas?
 
S

Sprechen sie von C++

Victor Bazarov said:
I was looking to massage some code, wanted to add matrix inversion to my
matrix template and related functions. [..]
Ideas?

Uh, how does "go google it" sound? If you don't like Google, use any
other internet search engine. And next time start by reading the FAQ.

I tried that, nothing but old crappy code that is hard to figure out.

I am trying to make a fresh start with all new metaprogramming techniques
 
F

Francesco S. Carta

Sprechen sie von C++ said:
I was looking to massage some code, wanted to add matrix inversion to my
matrix template and related functions.  [..]
Ideas?
Uh, how does "go google it" sound?  If you don't like Google, use any
other internet search engine.  And next time start by reading the FAQ..

I tried that, nothing but old crappy code that is hard to figure out.

I am trying to make a fresh start with all new metaprogramming techniques

Ah, I love to see fresh new approaches! Can I see some of the snippets
you're currently feeding to your compiler? I'd like to give a shot at
implementing the matrix inversion, if you don't mind sharing...
 
S

Sprechen sie von C++

Try this little tidbit.

// Useful for Gauss-Siedel and Jacobi, no pivoting with Guass Elimination
template <typename base> bool diagonal_dominant(matrix<base> a) {
base sum;
for(int i=0; i<a.size(); i++) {
sum = -fabs(a);
for(int j = 0; j<a.size(); j++)
sum += (base)fabs(a[j]);
if (abs(a)<sum) return false;
}
return true;
}
 
F

Francesco S. Carta

Sprechen sie von C++ said:
Try this little tidbit.

// Useful for Gauss-Siedel and Jacobi, no pivoting with Guass Elimination
template <typename base> bool diagonal_dominant(matrix<base> a) {
        base sum;
        for(int i=0; i<a.size(); i++) {
                sum = -fabs(a);
                for(int j = 0; j<a.size(); j++)
                        sum += (base)fabs(a[j]);
                if (abs(a)<sum) return false;
    }
        return true;

}


At a glance, I would prefer:

sum += base(fabs(a[j]));

furthermore, I would have completely avoided any conversion, and
diagonal_dominant would have been a member of matrix, but I suppose
that's matter of stylistic preferences.

Mind sharing something more, so that we can discuss about the overall
structural approach?
 
F

Francesco S. Carta

Sprechen sie von C++ said:
Try this little tidbit.

// Useful for Gauss-Siedel and Jacobi, no pivoting with Guass Elimination
template <typename base> bool diagonal_dominant(matrix<base> a) {
        base sum;
        for(int i=0; i<a.size(); i++) {
                sum = -fabs(a);
                for(int j = 0; j<a.size(); j++)
                        sum += (base)fabs(a[j]);
                if (abs(a)<sum) return false;
    }
        return true;

}


Wait, why are you casting that float to "base" in one case, when
you're implying the implicit conversion in other two cases? Is that a
joke?
 
S

Sprechen sie von C++

Wait, why are you casting that float to "base" in one case, when
you're implying the implicit conversion in other two cases? Is that a
joke?

I design my code to be equally flexible with float, double, complex<base>
and even arbitrary precision.

I have an arbitrary precision integer class that I hold out as an example.

I still have some concerns about the fabs but for now its all I have to work
with. The integer package has a overload so its no a big deal.
 
F

Francesco S. Carta

Sprechen sie von C++ said:
I design my code to be equally flexible with float, double, complex<base>
and even arbitrary precision.

I have an arbitrary precision integer class that I hold out as an example.

I still have some concerns about the fabs but for now its all I have to work
with. The integer package has a overload so its no a big deal.

That does not justify the fact that you're relying on implicit
conversion in two lines and using a cast on a third one, all in the
same function - not to speak that you shouldn't be casting at all.

If it were up to me, I would define an abs() template function and get
rid of any issue.

I'm not sure whether you missed my previous message, I'd like to see
your matrix implementation and see if a different approach could be
applicable - as I said there, I would implement all those functions as
members.
 
S

Sprechen sie von C++

This version of the wheel has been reinvented already. Google for "boost
ublas matrix inversion".

I have no use for the boost library. It so stuff with fluff its hard to use.

The boost idea is all fine and dandy but its gone over the top

It would be better if they had a set of headers that were better thought
out.
 
F

Francesco S. Carta

Sprechen sie von C++ said:
Feel free to post one.

I'm sure there is no need to post such a simple template. Actually,
those were all just suggestions about the approach to take.

If you're not going to discuss the approach and/or post your
implementations here I am not able to see any useful outcome for all
of this.
 
Ö

Öö Tiib

Sprechen sie von C++ ha scritto:




Well, then just install uBLAS and do not install the rest. The Boost
libraries are designed according to the "don't pay for what you don't
need" principle.

If I'm not very much mistaken, uBLAS is even one of the header-only
libraries, so "installing" comes down to copying some files and adding
one include path to your development environment.

As far as I am concerned, uBLAS is not hard to use at all. I once needed
some matrix calculations in a program and could immediately figure out
how to use the library and how to integrate it in my own code. Surely
beats designing, implementing and testing my own matrix library.

The trouble with ublas is that it is well ... 5000+ LOC in some
particular headers. matrix.hpp was around 4000 LOC when i last
checked. Such size is sort of on border between "over-ripe" and
"smelly" so i won't argue with anyone who refuses to use it because of
"too stuff with fluff".

If all goes well and ublas/matrix behaves within your use-case, it is
OK. If something fires back then happy digging there. Every piece of
software, including boost templates contains bugs.
 
S

Sprechen sie von C++

I'm sure there is no need to post such a simple template. Actually,
those were all just suggestions about the approach to take.

If you're not going to discuss the approach and/or post your
implementations here I am not able to see any useful outcome for all
of this.

template <typename T> T& fabs(T& ls, T& rs) {
}

Is this what you had in mind?
 
S

Sprechen sie von C++

The trouble with ublas is that it is well ... 5000+ LOC in some
particular headers. matrix.hpp was around 4000 LOC when i last
checked. Such size is sort of on border between "over-ripe" and
"smelly" so i won't argue with anyone who refuses to use it because of
"too stuff with fluff".

If all goes well and ublas/matrix behaves within your use-case, it is
OK. If something fires back then happy digging there. Every piece of
software, including boost templates contains bugs.

I have enough trouble with my own software defects so the last thing I need
is a dodgy 3rd party library.

Today I enhanced my matrix template, I added getcol/setcol so that I could
implement different algorithms in parallel.

The advantage with my code, I have the source and I can fix it if its
broken.

My tensor.h file is 369 lines as of today and its already mighty powerful.
That includes vector, matrix and some functionality to boot.
 
F

Francesco S. Carta

Sprechen sie von C++ said:
template <typename T> T& fabs(T& ls, T& rs) {

}

Is this what you had in mind?

Of the two, one:
- You haven't realized that such a function should take just an
argument - and that would mean that you have no clue and you're trying
to implement something above your head (unlikely, but possible).
- You missed to get the arguments right on purpose, trying to get some
fun out of this thread, and that would mean that you really have no
clue.

In either case, I'm out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top