Register to reply

Mathematica function writing

by izzy93
Tags: function, mathematica
Share this thread:
Feb24-13, 11:18 AM
P: 30
Hello all,

It's quite a mouthful but I need help writing a function that will behave as follows:
1.if called with an argument which is an integer, it will return the absolute value of the input
2. if called with a real argument, it will return the number rounded to the nearest integer if the argument is positive, zero if the argument is negative.
3. if called with a complex argument, it will round the real part and the imaginary part to the nearest integer and return the modulus of the result.

I starting with something like this.
rr = {a_integer -> Abs[a], a_real -> If[a_real < 0, 0, Integer[a]],
a_complex ->

Then I found the 'which' function
ff[a_] = Which[integer[a], Abs[a], a_Real,
If[a_real < 0, 0, Integer[a]],
a_Complex, {Real[a] & Im[a] -> Mod[integer[a]] }]

However I don't think I'm inputting things correctly.

Any tips would be appreciated

Phys.Org News Partner Science news on
An interesting glimpse into how future state-of-the-art electronics might work
Tissue regeneration using anti-inflammatory nanomolecules
C2D2 fighting corrosion
Bill Simpson
Feb24-13, 07:40 PM
P: 1,037
I'm guessing this is homework, so I'll just provide hints.
It this REALLY isn't homework then convince me and I'll hand it to you.

There is no integer[] function.
Your a_Real and a_Complex are not doing what you may think they are.
Your Mod[] is not doing what you may think it is.

Using := instead of = will help you
IntegerQ[] will help you.
Im[]==0 will help you.
Combining two tests using And[] or && will help you.
Start just implementing part of the requirements, carefully test to see that is working and then add more.
Feb25-13, 12:56 AM
PF Gold
Hepth's Avatar
P: 470
Also, one thing you may be missing is "element", read the help on it.

Feb25-13, 07:59 AM
P: 44
Mathematica function writing

Piecewise function / Mathematica Documentation /
Look up this function and if it suits you try to combine it with the above hints by Bill & Hepth.
Post your results :)
Feb27-13, 09:18 AM
P: 30
Hello everyone

Thanks for replying.

Taking Bills comments on board ( althoughh not sure why Im==0 will be handy?) and using the If function I came up with the following. although still likely gaping errors.

rr[a_] := [{If[IntegerQ[a_], Abs[a], a] && Re[a_] ->
If[a > 0, Round[a], 0] && Complex[a_] ->
Det[Round[Real[a]] && Round[Im[a]]]

Using the piecewise function;

rr[a_] := Piecewise[{{Abs[a], IntegerQ[a]}, {Round[a],
Re[a] /; a >= 0}, {0,
Re[a] /; a < 0}, {Abs[Round[Re[a] && Im[a]]], Complex[a]}}]

which doesn't work but seems good.

The element function could be handy for stating what argument a is eg complex, real or integer. But isn't that done already in specifying conditions using the above mathematica functions.

Thanks for you're help,

P.s it is homework
Bill Simpson
Feb27-13, 05:29 PM
P: 1,037
You are making a little progress.
Some more tips:

If you have a_ on the left of your := then you almost always don't want a_ on the right, you almost always want just a on the right.

Your If[] seems a little mixed up. Perhaps think of it this way
If[trueORfalseExpression,valueIfItWasTrue, valueIfItWasFalse]
or just
and it returns nothing or null if it was not true.

In Mathematica almost EVERYTHING is a function, even If[] is a function, similar to the way that Sin[x] is a function. Some of the functions don't return anything useful, but almost everything is still a function. If you keep that in mind then some things may begin to make a little more sense as you learn more about this truly strange language.

Your -> is usually used for pattern matching substitution, which you don't seem to be doing.

Perhaps your original Which[] might make this easier for you.

Can you then think of a specific true or false test to do first?
And the result to return if that is true?
And then the next specific true or false test to do if the first test was false?
And the result to return if that second test was true?

You are trying to eliminate one category of inputs and provide a result for those on each line.

Does that help.

Awfully nice of you to admit it was homework. I realize too many people today just want someone to do their homework for them.
Feb28-13, 05:17 PM
P: 30
Hello, Thanks for that lengthy reply, It's most helpful.

I've now got something like this

ff[a_] := Which[IntegerQ[a], Abs[a], Element[a, Reals],
If[a < 0, 0, Integer[a]], Element[a, Complexs],
Abs[Round[Re[a] && Im[a]]]]

still a bit dodgy I think.

Bill Simpson
Feb28-13, 09:43 PM
P: 1,037
You are showing you can correctly understand a good hint and figure out how to use it. That is excellent.

More tips.

See if Mathematica provides the "Integer" function you are using or not and if not then use a bit of thinking and perhaps a bit of guessing to make your next step.

Look up Mathematica's definition of the Round function and what kind of arguments it accepts. Then think about what kind of argument you are giving it. Is what you are doing with that correct or not? Likewise for the Abs function.

Then explore using various inputs to your ff function to test each kind of possible input that it is supposed to accept. Verify which kinds of inputs are giving correct results. Make sure to test "boundaries" carefully. For example, if it is supposed to do one thing with a positive input and a different thing with a negative input then make sure that it does the right thing when given something on the boundary between those two, zero.

Keep up the good work and I think you will perhaps do well with this.
Mar1-13, 03:36 AM
P: 30
ff[a_] :=
Which[IntegerQ[a], Abs[a], Element[a, Reals],
If[a < 0, 0, IntegerPart[a]],
Element[a, Complexs], {Abs[Round[Re[a]]]} && Abs[Round[Im[a]]]]

It now works for getting the real number rounded to the nearest integer if positive.It doesn't make it 0 if -ve though.

As far as I can see Abs is the only function to use and the inputs for that and Round are ok.

Bill Simpson
Mar2-13, 09:22 PM
P: 1,037
Complexs is misspelled. Mathematica is FANATIC about exactly correct spelling and capitalization and will give you nothing, error messages you probably won't know what to do with or incorrect answers with the tiniest error in this.

In[1]:= ff[a_]:=Which[


Out[2]= {{-5, 5}, {-1, 1}, {0, 0}, {1, 1}, {5, 5},
{-5.6, 0}, {-5.5, 0}, {-5.4, 0},
{5.4, 5}, {5.5, 5}, {5.6, 5},
{-5.6 - 5.6*I, {6} && 6}}

It doesn't look to me like the third line of output satisfies the specification you originally gave and you should be able to figure out what to do to fix that.
It doesn't look to me like the fourth line of output satisfies the specification and perhaps studying the definition of modulus for complex numbers will enable you to figure out what to do to fix that.

Then you should try lots more test cases to verify all the special cases and boundary conditions you can think of.

You are making considerable progress.
Mar4-13, 10:07 AM
P: 30
Thanks for all you're help,

Think I got it now

Register to reply

Related Discussions
Writing an absolute value function as a piecewise function Precalculus Mathematics Homework 2
Writing a Farey sequence in Mathematica. General Math 1
I need help writing a mathematica function. Linear & Abstract Algebra 5
Can anybody help me, who is good with Mathematica, with writing a sequence. Math & Science Software 1
Give to an interpolation function an analitic function with mathematica Math & Science Software 1