# Extracting a function from a standard

I have a standard and I need create a function to resolve this, for example:

if my $X < 21$ my $Y$ will be $24$ else if my $x < 28$ my $y$ will be $32$ ...

How do I calculate a function for this?

Thanks all.

-
@Rasmus the standart is what i want, for example here x < 21 → y = 24 and (x+7) < (21+7) → y = (24+8) ... – ademar111190 Aug 10 '12 at 14:35
You ask [if (x<21) y=24; else if (x<28) y=32;] but can y really only have these 2 values ? (and what if x>=28 ?) – Raymond Manzoni Aug 10 '12 at 14:35
@RaymondManzoni not, the y will vary between 17 values. and x between n values. – ademar111190 Aug 10 '12 at 14:37
you could create an array of sorted (maximal x, corresponding y) (or two arrays) and parse it in a function with sequential or binary search (rather a question for stack overflow...). But the ifs could be faster... – Raymond Manzoni Aug 10 '12 at 14:44

You can simply define the function in piecewise terms over its domain:

$$y=f(x) = \left\{\begin{array}{cc} 24, & \mathrm{if}\ x < 21, \\ 32, & \mathrm{if}\ 21 \le x < 28. \\ \end{array}\right.$$

As to whether there is a pattern herein, there is not enough information to decide.

Another way is to use indicator functions, which are defined as $$\mathbf{1}[\chi] = \left\{\begin{array}{cc} 1, & \mathrm{if}\ \chi\ \mathrm{is\ true}, \\ 0, & \mathrm{if}\ \chi\ \mathrm{is\ false}.\end{array}\right.$$

Them you can write $$y=f(x) = 24\cdot\mathbf{1}[x < 21] + 32\cdot\mathbf{1}[21 \le x < 28] + \cdots$$

In programming, sometimes indicator functions are called "boolean variables" (where you might have to map $\mathrm{true} \mapsto 1$, $\mathrm{false} \mapsto 0$.)

-
Whoops! Misread it. Editing, thanks! – Emily Aug 10 '12 at 14:33
thanks, but I have 17 ifs in my real problem, and i'm implementing this in java, to do so is bad because wiil be 17 ifs in code, very bad, don't have a other way? – ademar111190 Aug 10 '12 at 14:33
That is a programming question. There are likely many other ways to implement this using mathematical functions; however, you have given only a small subset of your problem. Furthermore, is the code really more readable if you implement a complicated mathematical function than using obvious heuristics? At any rate, I will edit my answer to specify another approach. – Emily Aug 10 '12 at 14:36
@EdGorcenski thanks for light I will in this way. really with the function had become less clear but will be faster and with a appropriate comment had been easy to understand. – ademar111190 Aug 10 '12 at 14:42
@ademar: this should be asked on one of the programming sites. Anyways... (1) unless you have good reason to think that it's important for this function to be faster, you should stick to whatever is more clear. (2) you can do clever things with chained if's: you can write them in the order they're most likely to be satisfied, or nest them as a binary search, or other sorts of things. (3) You can encode the relevant data in other fashions, such as having an array of <upper bound, value> pairs, and searching the array for the appropriate value. (4) et cetera – Hurkyl Aug 10 '12 at 15:12
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 1, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "http://math.stackexchange.com/questions/181044/extracting-a-function-from-a-standard", "fetch_time": 1469494823000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2016-30/segments/1469257824499.16/warc/CC-MAIN-20160723071024-00010-ip-10-185-27-174.ec2.internal.warc.gz", "warc_record_offset": 158190766, "warc_record_length": 18484, "token_count": 941, "char_count": 3148, "score": 3.875, "int_score": 4, "crawl": "CC-MAIN-2016-30", "snapshot_type": "latest", "language": "en", "language_score": 0.7589627504348755, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00020-of-00064.parquet"}