Author Topic: ---={ Challenge }=--- Find inverse function  (Read 6882 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3245
  • Marko Ribar, architect
---={ Challenge }=--- Find inverse function
« on: April 27, 2020, 09:39:52 AM »
Hi all...
Long time no challenges asked... This one is really tough and I think that IMHO is still impossible to be solved...
Now to get to the point. Through long period of time I coded in ALISP and very often I came into situation where I have to use math or some programming skills to write and translate real formula or pseudo code to ALISP defined function either by using (defun), (lambda) - defun-q I use rarely... Now if you take this simple formula A=B+3 and you translate it into this :

(defun a ( b )
  (+ b 3)
)

Result of (a 2) would be 5... That's fine... But what if formula is very complex and not so evident and you still have USUBR a which will operate the same (a 2) => 5... Now the goal of this challenge is to get USUBR ainv which will give (ainv 5) => 2 ...

So you have defined function - it could be as simple as (chr 65) => "A" ... Now we need to get (ascii) function from (chr), so that (chrinv "A") => 65 - the same as (ascii "A") => 65...

Let's see if some eminent person like Lee Mac, or MP can do it... For a start I am narrowing things to only pure math and math formula where you define function with 1 number argument and 1 return as number and the goal is to get inverse function also with 1 number argument and 1 return as number...

Thanks for reply in advance...
Regards, M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: ---={ Challenge }=--- Find inverse function
« Reply #1 on: April 27, 2020, 10:47:13 AM »
I posted a few routines called HOOK and LET many years ago that was related to this subject. -i.e. should be simple enough to build a function to construct functions.

Code - Auto/Visual Lisp: [Select]
  1. (defun hook (name arg func)
  2.   (eval (list 'defun name (list arg) func)))
  3.  
  4. (hook 'foo 'arg '(+ arg 3))
  5. (hook 'fooinv 'arg '(- arg 3))

Code: [Select]
Command: (hook 'foo 'arg '(+ arg 3))
FOO

Command: (hook 'fooinv 'arg '(- arg 3))
FOOINV

Command: (foo 2)
5

Command: (fooinv 5)
2
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ribarm

  • Gator
  • Posts: 3245
  • Marko Ribar, architect
Re: ---={ Challenge }=--- Find inverse function
« Reply #2 on: April 27, 2020, 11:26:57 AM »
My simple solution considers involving global variables like *r*

Code - Auto/Visual Lisp: [Select]
  1. (defun f ( a / b )
  2.   (setq b (+ a a (/ a 2.0) (expt a 2))) ; place your formula with a
  3.   (setq *r* (- b a))
  4.   b
  5. )
  6.  
  7. (defun finv ( b )
  8.   (- b *r*)
  9. )
  10.  

But this is not what I was looking for in generalized terms... This is just math approach like I proposed in my finishing statement, but the problem is (chr 65) => "A" and (chrinv "A") => 65 in general
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: ---={ Challenge }=--- Find inverse function
« Reply #3 on: April 27, 2020, 11:50:03 AM »
My simple solution considers involving global variables like *r*
Code: [Select]
(f 2)
(finv 9)
(finv 26)
(f 4)

ribarm

  • Gator
  • Posts: 3245
  • Marko Ribar, architect
Re: ---={ Challenge }=--- Find inverse function
« Reply #4 on: April 27, 2020, 12:07:44 PM »
My simple solution considers involving global variables like *r*
Code: [Select]
(f 2)
(finv 9)
(finv 26)
(f 4)

I don't get it... What do you want to say?
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: ---={ Challenge }=--- Find inverse function
« Reply #5 on: April 27, 2020, 12:20:58 PM »
I'm very confused by this thread.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ribarm

  • Gator
  • Posts: 3245
  • Marko Ribar, architect
Re: ---={ Challenge }=--- Find inverse function
« Reply #6 on: April 27, 2020, 12:50:02 PM »
My simple solution considers involving global variables like *r*
Code: [Select]
(f 2)
(finv 9)
(finv 26)
(f 4)

I don't get it... What do you want to say?

I see the point... (finv) is not real inverse function of (f), but it's attempt to make connection between user defined function and return value... It works as inverse only if (f) is executed firstly and BTW. If (f) was never executed *r* would be nil and therefore and (finv) would break with error and fail...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3245
  • Marko Ribar, architect
Re: ---={ Challenge }=--- Find inverse function
« Reply #7 on: April 27, 2020, 12:56:36 PM »
I'm very confused by this thread.

No, the topic is very useful if solution is to be found... It looks complicated, but in fact solution should be simple - someone should be able to create USUBR of already defined USUBR in the way that it operates reversed then it should... No need for formulas - just crack of foo to fooinv...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: ---={ Challenge }=--- Find inverse function
« Reply #8 on: April 27, 2020, 01:32:06 PM »
I see the point...
yep
that's what i've meant

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: ---={ Challenge }=--- Find inverse function
« Reply #9 on: April 27, 2020, 01:33:41 PM »
Code: [Select]
(setq f_backup f)
(defun f (a) (setq *r* a) (f_backup a))
(defun f_inv () *r*)

ribarm

  • Gator
  • Posts: 3245
  • Marko Ribar, architect
Re: ---={ Challenge }=--- Find inverse function
« Reply #10 on: April 27, 2020, 01:33:52 PM »
Here is another one based on *r*... Maybe this one is better...

Code - Auto/Visual Lisp: [Select]
  1. (defun f ( a / b )
  2.   (setq b (chr a)) ;; Place your formula with a here...
  3.   (setq *r* (list b a))
  4.   b
  5. )
  6.  
  7. (defun finv ( b )
  8.   (if (equal b (car *r*))
  9.     (cadr *r*)
  10.   )
  11. )
  12.  

Code: [Select]
(f 65) => "A"
(finv "A") => 65
(f 85) => "U"
(finv "U") => 85

Also (f) must be firstly initialized for (finv) to work as desired...
« Last Edit: April 27, 2020, 01:46:44 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12910
  • London, England
Re: ---={ Challenge }=--- Find inverse function
« Reply #11 on: April 27, 2020, 01:47:07 PM »
This is impossible in general.

A simple example would be attempting to write an inverse hashing function.

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: ---={ Challenge }=--- Find inverse function
« Reply #12 on: April 27, 2020, 01:59:51 PM »
This is impossible in general.
i guess Marko just wants to know with what argument a function was called

ribarm

  • Gator
  • Posts: 3245
  • Marko Ribar, architect
Re: ---={ Challenge }=--- Find inverse function
« Reply #13 on: April 27, 2020, 02:00:32 PM »
I was not referring to inverse function of complex function that is composed of other functions, just straight forward function with single formula and single argument and single return...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3245
  • Marko Ribar, architect
Re: ---={ Challenge }=--- Find inverse function
« Reply #14 on: April 27, 2020, 02:02:09 PM »
This is impossible in general.
i guess Marko just wants to know with what argument a function was called

Not exactly... That among other things, but I really want inverse function of function...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube