Defining my own Dirichlet like function

piecewise[{{0, for x rational}, {x, for x irrational}}]

I’ve tried this one. but with no success. can someone help me?

In[1]:= d[x_] := Piecewise[{{0, Head[x] === Rational}}, x];
d[3/4]
d[E]

Out[2]= 0

Out[3]= E

I urge you to try to find ways that this doesn’t work.

@BillSimpson Hmm… let’s see… how about d[1] ? Your code returns 1 for it. :slight_smile:

In addition to treating integers as rationals, following might be more amenable to symbolic analysis by Mathematica:

d[x_] := Piecewise[{{0, Element[x, Rationals]}}, x];

That was the easy obvious one which I hoped the hint would be sufficient for the original poster to recognize.

Are there more? I think I can see at least one additional class, and perhaps more which neither of these are sufficient to deal with.

Being more or less amenable to symbolic analysis by Mathematica was well beyond what I guessed the original poster, or most, might be able to address.

@BillSimpson

My version doesn’t recognize complexes even if Re and Im parts are rational. I’m not certain if original poster was interested of that, either.

Also, my version leaves function for Real numbers (in the Mathematica sense; that is, approximate numbers) unevaluated. This is understandable from the viewpoint that all such finite and well-defined numbers are non-point intervals, and all such intervals contain both rational and irrational numbers. Since Mathematica can’t say that all or none of them are on Rationals, it leaves the case unevaluated.

Third case is probably some non-closed-form values or arguments to exotic built-in functions that Mathematica is unable to reason to get exact value, or even the strictest domain it is in. In case of these, result would once again be left unevaluated, although more forciful FullSimplify often helps.

I guess whatever you referred to as “at least one additional” class was not yet mentioned by me…

First of all thank you all for replying to my question in such a short time.
I guess I wasn’t clear enough. My goal is to define this function in wolframalpha.com site and not in Mathematica.
How do I type this formula in the “calculate” field. I just need to see its graph.

First of all thank you all for replying to my question in such a short time.
I guess I wasn’t clear enough. My goal is to define this function in wolframalpha.com site and not in Mathematica.
How do I type this formula in the “calculate” field. I just need to see its graph.

Its graph looks like the constant 0 function, or 1 function, depending on how you look at it. It could look like both superimposed, if you look at it like that. Both sets are dense in [0,1], so there aren’t any gaps - just both the lines.

but how do I define this in wolframalpha.com?

  1. In Mathematica is there an example where Head === Rational and Element[x, Rationals] differ?

We might be able to guess the implementation of the first, but the second might be more open to question.
Are they actually identical or not?

  1. I tried an assortment of ways to get WolframAlpha to plot or even define your function and failed repeatedly.

In my experience, one of the differences between MMA and WA is that MMA allows function definitions and WA doesn’t,
but you can sometimes just insert the “body” of the function into your WA query and get around this.

If there is a recipe for how to efficiently discover the syntax to understand a particular WA query I’d love to learn that.

Note: I did accidentally stumble onto appending “last 10 years” onto the end of a WA query and found it is possible
to get the history of some statistic instead of just the latest value, I had long since given up and assumed that
this just could not be done. I still have not found a way to get an inflation adjusted history though.

@Bill

  1. It is not known whether E + Pi is rational or not, hence
In[1]:= Element[E + Pi, Rationals]
Out[1]= E + \[Pi] \[Element] Rationals

However

In[2]:= Head[E + Pi] === Rationals
Out[2]= False