473,785 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can a function returns a matrix?

hi all,
I want a function that takes matrix as input and returns a
matrix. (say, function that performs matrix multiplication) . How can I
go about this? Can anybody help me?
Balaji.V

Aug 2 '06
12 4854
In article <eb**********@n ews.tiscali.fr>
Bernard Xhumga <xh****@tiscali .frwrote:
>Now [this code] is perfect ?
It seems nothing is ever *perfect* but at least it is all correct :-)

[snippage]
>double **A = malloc(r * sizeof( *A) );
if(!A)exit(1);
In a more-general-purpose routine, it would be fairly rude to exit
(with no error message, at that) here -- instead, the function
should return some sort of error indication to its caller, saying
"I was unable to allocate the memory you requested". For quite a
few purposes, simply exiting (preferably with a message first) is
sufficient, though.
A[0] = malloc(r * c * sizeof(**A) );
if(!A[0])exit(1);
This part is good enough, with the same caveat.
for(i=1; i<r; i++)
{
A[i] = A[0]+i*c;
if(!A[i])exit(1);
This test ("if(!A[i])") is unnecessary, though harmless. If A[0]
is not NULL, A[0] + i * c should never be NULL either.
>return(A);
}
[snippage]
>void f_mR(
double **A
)
{
free((void *)A[0]);
free((void *)A);
}
The two casts are unnecessary but harmless (since <stdlib.hwas
included in code I snipped above).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Aug 7 '06 #11
Chris Torek <no****@torek.n etwrites:
In article <eb**********@n ews.tiscali.fr>
Bernard Xhumga <xh****@tiscali .frwrote:
>>Now [this code] is perfect ?

It seems nothing is ever *perfect* but at least it is all correct :-)

[snippage]
>>double **A = malloc(r * sizeof( *A) );
if(!A)exit(1);

In a more-general-purpose routine, it would be fairly rude to exit
(with no error message, at that) here -- instead, the function
should return some sort of error indication to its caller, saying
"I was unable to allocate the memory you requested". For quite a
few purposes, simply exiting (preferably with a message first) is
sufficient, though.
[...]

And if you do want to call exit(), use exit(EXIT_FAILU RE) rather than
exit(1); the latter is non-portable, and may not indicate failure on
some systems.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 7 '06 #12
Thank for your help.
I have learned a lof of in few time.
Now I will try to rewrite my work.

Thank.

Aug 7 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1488
by: Matt Feinstein | last post by:
Is there an optimal way to apply a function to the elements of a two-d array? What I'd like to do is define some function: def plone(x): return x+1 and then apply it elementwise to a 2-D numarray. I intend to treat the function as a variable, so ufuncs are probably not appropriate-- I
2
3006
by: Ryan Mitchley | last post by:
Hi all I have the functions friend CComplexMatrixTemp eye(const size_t nN); and friend CComplexMatrixTemp & chol(CComplexMatrixTemp &z); I would like create expressions of the form CComplexMatrixTemp x = chol(eye(6)), similar to MATLAB syntax.
3
1395
by: Craig Nicol | last post by:
Hi, Although I've been using C++ for a while, I've only recently started writing my own template classes so forgive me if this is a silly question. I have a matrix class, mgMatrix, that is templatised so that it can be a matrix of any type. It has three data members, two integers for the number of rows and columns in it, and a vector of size rows*columns to hold all the data.
3
1474
by: bluekite2000 | last post by:
I d rather have a Matlab syntax such as: // create a Matrix of size 3,4 and fills it w/ random values. Matrix<double> M=rand(3,4) But since this function changes the state of the object, I ought to make it a member function. ie.
4
1400
by: Jason | last post by:
/* I have never use template before, so bear with me. Here is what I am trying to do: I declare an abstract base class MatrixInterface with template to define an interface for all my subsequent Matrix class. In MatrixInterface class, I overloaded the << operator by calling a pure virtual function PrintDebugMessage(ostream &os); then I can implement the function on individual Matrix classes later.
7
1916
by: kulpojke | last post by:
I am trying to map a function to the contents of an array using the map() function. However, whenever the following function is called an error message is returned complaining that: ... line 121, in b_maker b = map(function,b) TypeError: 'matrix' object is not callable This is on a windows32 system, using python 2.5 with the numpy library. In the following code data is a nested list of type float, target is a list of type float...
1
2215
by: joeedh | last post by:
Hi I'm getting extremely odd behavior. First of all, why isn't PyEval_EvalCode documented anywhere? Anyway, I'm working on blender's python integration (it embeds python, as opposed to python embedding it). I have a function that executes a string buffer of python code, fetches a function from its global dictionary then calls it. When the function code returns a local variable, PyObject_Call() appears to be returning garbage. ...
5
1679
by: Envergure | last post by:
I wrote a function to find the point of intersection of a line and a plane in three-space. This function works fine and returns the correct result. However, when I call it using an element from an array of triangles for the first param I get a compile error: error: no match for call to ‘(point) (triangle&, line&)’ Any ideas what may be causing this? Compiled (or not) with G++: g++ -Wall "MovesShootDetect.cpp" -lGL -lGLU `sdl-config...
8
1763
by: aeneng | last post by:
Hello everyone, I am just starting to use python in numerical cacluation. I need you to help me to see what's wrong with the following piece of codes, which computes the cross product of two vectors and returns the result. u and v are two 3x1 matrix. when I import the function, error message show like this Traceback (most recent call last): File "<stdin>", line 1, in ?
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9952
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.