Octave – valutazione – 40


Oggi, continuando da qui, sono giunto qui.

Valutazione delle espressioni
Normally, you evaluate expressions simply by typing them at the Octave prompt, or by asking Octave to interpret commands that you have saved in a file.
Sometimes, you may find it necessary to evaluate an expression that has been computed and stored in a string, which is exactly what the eval function lets you do.

Built-in Function: eval (try)
Built-in Function: eval (try, catch)

If execution fails, evaluate the optional string catch.
The string try is evaluated in the current context, so any results remain available after eval returns.
The following example creates the variable A with the approximate value of 3.1416 in the current workspace.


Notare i diversi modi di scrittura; l’ultima è più tradizionale, quella usata in altri linguaggi.

If an error occurs during the evaluation of try then the catch string is evaluated, as the following example shows:


Programming Note: if you are only using eval as an error-capturing mechanism, rather than for the execution of arbitrary code strings, Consider using try/catch blocks or unwind_protect/unwind_protect_cleanup blocks instead. These techniques have higher performance and don’t introduce the security considerations that the evaluation of arbitrary code does.

Pronto a passare qui.

Chiamare una funzione per nome
The feval function allows you to call a function from a string containing its name. This is useful when writing a function that needs to call user-supplied functions. The feval function takes the name of the function to call as its first argument, and the remaining arguments are given to the function.

The following example is a simple-minded function using feval that finds the root of a user-supplied function of one variable using Newton’s method. No, non messa: non sono riuscito a farla funzionare e non ho trovato esempi simili googlando.

Built-in Function: feval (name, ...)
Evaluate the function named name.
Any arguments after the first are passed as inputs to the named function. For example, calls the function acos with the argument ‘-1


The function feval can also be used with function handles of any sort (see Function Handles).
Historically, feval was the only way to call user-supplied functions in strings, but function handles are now preferred due to the cleaner syntax they offer. For example,


are equivalent ways to call the function referred to by f. If it cannot be predicted beforehand whether f is a function handle, function name in a string, or inline function then feval can be used instead.

A similar function run exists for calling user script files, that are not necessarily on the user path

Command: run script
Function File: run ("script")

Run script in the current workspace.
Scripts which reside in directories specified in Octave’s load path, and which end with the extension “.m“, can be run simply by typing their name. For scripts not located on the load path, use run.
The file name script can be a bare, fully qualified, or relative filename and with or without a file extension. If no extension is specified, Octave will first search for a script with the “.m” extension before falling back to the script name without an extension.
Implementation Note: If script includes a path component, then run first changes the working directory to the directory where script is found. Next, the script is executed. Finally, run returns to the original working directory unless script has specifically changed directories.

Ecco lo script p-run:

# script p-run
# per i piemontesi: no nessun coniglio

n = 6 * 7
l = log(n)
r = exp(l)



lo stesso script rinominato c_run.m viene eseguito in questo modo:


Nota perso: ovvio che il nome non può contenere caratteri come -, +, *, …

Avanti, passo qui.

Valutazione in un diverso contesto
Before you evaluate an expression you need to substitute the values of the variables used in the expression. These are stored in the symbol table. Whenever the interpreter starts a new function it saves the current symbol table and creates a new one, initializing it with the list of function parameters and a couple of predefined variables such as nargin. Expressions inside the function use the new symbol table.
Sometimes you want to write a function so that when you call it, it modifies variables in your own context. This allows you to use a pass-by-name style of function, which is similar to using a pointer in programming languages such as C.

Consider how you might write save and load as m-files. For example:


With evalin, you could write save as follows:


Here, ‘caller’ is the create_data function and name1 is the string "x", which evaluates simply as the value of x.
You later want to load the values back from mydata in a different context:

function process_data
  load mydata
  ... do work ...

With assignin, you could write load as follows:


Here, ‘caller’ is the process_data function.
You can set and use variables at the command prompt using the context ‘base’ rather than ‘caller’.
These functions are rarely used in practice. One example is the fail (‘code’, ‘pattern’) function which evaluates ‘code’ in the caller’s context and checks that the error message it produces matches the given pattern. Other examples such as save and load are written in C++ where all Octave variables are in the ‘caller’ context and evalin is not needed.

Alquanto misterioso, occorrerebbe provare in un caso particolare ma pare si usi raramente, praticamente mai.

Built-in Function: evalin (context, try)
Built-in Function: evalin (context, try, catch)

Like eval, except that the expressions are evaluated in the context context, which may be either "caller" or "base".

Built-in Function: assignin (context, varname, value)
Assign value to varname in context context, which may be either "base" or "caller".


Posta un commento o usa questo indirizzo per il trackback.



Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: