Hints for a string puzzle (symbolic math)

12 views
Skip to first unread message

gamo

unread,
Jun 15, 2021, 8:44:04 AMJun 15
to

I post the draft OSS for a kind of auto clear of a $var
from a string.

Any help or hint is welcome. No, I don't like a OOP orientation.

#!/usr/bin/perl -w

use strict;

my $m = 110;
my $E = 1_000_000_000;
my $c; # incógnita

# The aim of this OSS is to put a list of macroeconomic equations and
expand them
# for all variables. Therefore giving a number of values to vars we
could make predictions
# or do simulations around the total block of equations.

my %eqs = (
"1" => "$E = $m*$c**2", # El objetivo es poner
varias ecuaciones y expandirlas en todas las $var
);

my @l = search ('$c');

for my $i (@l){
print clear('$c',$i), ";\n";
}

exit 1;


sub search {
my $var = shift;
my @result;
while (my ($k,$v) = each %eqs){
if ($v =~ /\Q$var/i){
push @result , $k;
}
}
return @result;
}

sub clear {
my ($var, $e) = @_;
my $eq = $eqs{$e};

my %antiop = (
"=" => "=",
"+" => "-",
"-" => "+",
"*" => "/",
"/" => "*",
"**" => "**1/",
"**1/" => "**",
"**2" => "**0.5",
);

my %prio = (
"=" => 0,
"+" => 1,
"-" => 1,
"/" => 2,
"*" => 3,
"**" => 4,
);

my ($n,$d);

my $s = qr/[\=|\*|\+|\-|\/]/;

goto MEJOR;

my @ss;
push @ss, $1 while ($d =~ /($s)/g); # list of ops, ordered left -
right

my @t2;
push @t2, $1 while ( $d =~ /\((.+)\)/g ); # list of subeq inside ()

my @t = split /$s/, $d; # list of vars # PROBLEM
@t2 and the rest of $d

my @nums;
push @nums, $1 while ($d =~ /(\d+)/g); # list of numbers

MEJOR:

# ENFOQUE MEJOR:

# 1) simplificar la ecuación por bloques en torno al operador/es
principales
$eq =~ s/\s+//g;
my @term;
my @oper;
while ( $eq =~ /(\Q$s\E)?\(?(.+)\)?/g ){
push @oper, (defined $1) ? $1 : "";
push @term, $2;
}

# 2) identificar cuál/es tienen la $var objetivo
my ($count, @lista);
for (@term){
$count++;
if ($_ =~ /\Q$var\E/){
push @lista, $count-1;
}else{
push @lista, -($count-1); # No puede ser 0 porque el término 0 o no
existe o ya está despejado
}
if ($var eq $_){ return $eq; } # No hay que hacer nada, es la ecuación
original
}

# 3) despejar los bloques en los que no está la $var objetivo

$count=-1;
my $eq2;
my $flag;
for my $i (@term){
$count++;
if ($lista[$count]==0){
$eq2 = $i;
}elsif ($lista[$count] < 0) {
if (index ("(", $i) >=0){

}else{
$eq2 .= $antiop{ $oper[abs($count)] } . $i;
}
}elsif ($lista[$count] > 0) {
if (index ("(", $i) >=0){

}else{
$eq2 = $antiop{ $oper[$count] } . $eq2;
if ($eq2 =~ s/ ^\/ / 1\/ /x){
# problema de paréntesis y ordenación futuro
$flag = 1;
}
}
}
}

($n,$d) = split "=", $eq2;
$eq2 = "$d = $n";
return $eq2;

# 4) tratar los bloques en que está la $var objetivo -> goto 3)




}


--
http://gamo.sdf-eu.org/
perl -E 'say "Beware of cocodriles at the toll.";'

Eric Pozharski

unread,
Jun 18, 2021, 1:33:41 PMJun 18
to
with <saa7ac$cdc$1...@gioia.aioe.org> gamo wrote:
>
> I post the draft OSS for a kind of auto clear of a $var
> from a string.
>
> Any help or hint is welcome. No, I don't like a OOP orientation.

Hint: TB of yours wraps long lines (especially -- trailing comments),
that makes your smalest-complete-example invalid. Sooner or later
people will start to suppose PEBKAC, you don't want this.

> #!/usr/bin/perl -w
*SKIP*
> my $c; # incógnita
*SKIP*
> "1" => "$E = $m*$c**2", # El objetivo es poner

I suppose warnings already told you where it's going.

*SKIP*
> my $s = qr/[\=|\*|\+|\-|\/]/;

This isn't even wrong. I suppose worse (panic? in skipped, 'goto' is
effectevely the block comment).

*CUT*

Now, in general. As of OOP Denial Area -- this is stupid but noble,
nevertheless, goal (in some unspecified places aka -- conduct), no
objections here.

What this task requires are: first, parser; then algebra will emerge
naturally. Instead you merge parser and algebra together and press them
against scalar that is awfully similar to Perl (IOW, only perl can parse
Perl, not gonna work!).

--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom

gamo

unread,
Jun 18, 2021, 7:02:38 PMJun 18
to
El 18/6/21 a las 14:38, Eric Pozharski escribió:
> What this task requires are: first, parser; then algebra will emerge
> naturally. Instead you merge parser and algebra together and press them
> against scalar that is awfully similar to Perl (IOW, only perl can parse
> Perl, not gonna work!).

Yes, there are parsers in symbolic math modules that could produce trees.
But AFAIK there isn't the option to clear a variable, so I guess that
that modules could be bridged to do the task. Or telling the story
otherwise, I don't know how that trees could simplify the algorithm.

The eq strings are intentionally similar to Perl to be 'eval'ed.

Thank you very much.

--
http://gamo.sdf-eu.org/
perl -E 'say "Come to Foo bar";'

Eric Pozharski

unread,
Jun 19, 2021, 1:33:14 PMJun 19
to
with <saj8m6$unh$1...@gioia.aioe.org> gamo wrote:
> El 18/6/21 a las 14:38, Eric Pozharski escribió:

>> What this task requires are: first, parser; then algebra will emerge
>> naturally. Instead you merge parser and algebra together and press
>> them against scalar
*SKIP*
> Yes, there are parsers in symbolic math modules that could produce
> trees. But AFAIK there isn't the option to clear a variable, so I
> guess that that modules could be bridged to do the task. Or telling
> the story otherwise, I don't know how that trees could simplify the
> algorithm.

You're underestimating yourself. Of course you don't see algebra
emerging, but solely because you don't have the tree yet. Also, it
should be mentioned clearly -- that's you who will do all footwork, but
that's curse of the conduct.

*CUT*

gamo

unread,
Jun 19, 2021, 3:35:21 PMJun 19
to
El 19/6/21 a las 15:22, Eric Pozharski escribió:
> with <saj8m6$unh$1...@gioia.aioe.org> gamo wrote:
>> El 18/6/21 a las 14:38, Eric Pozharski escribió:
>
>>> What this task requires are: first, parser; then algebra will emerge
>>> naturally. Instead you merge parser and algebra together and press
>>> them against scalar
> *SKIP*
>> Yes, there are parsers in symbolic math modules that could produce
>> trees. But AFAIK there isn't the option to clear a variable, so I
>> guess that that modules could be bridged to do the task. Or telling
>> the story otherwise, I don't know how that trees could simplify the
>> algorithm.
>
> You're underestimating yourself. Of course you don't see algebra
> emerging, but solely because you don't have the tree yet. Also, it
> should be mentioned clearly -- that's you who will do all footwork, but
> that's curse of the conduct.
>
> *CUT*
>

Yes, you might be right. In particular, I don't sense the need for
parsing depth parentesees structures, but all these complicated work
is already done and you have to not to reinvent the wheel.

Thanks!

--
http://gamo.sdf-eu.org/
perl -E 'say "Something's wrong. I work too much."'

Eric Pozharski

unread,
Jun 20, 2021, 1:33:20 PMJun 20
to
with <salgtg$j2t$1...@gioia.aioe.org> gamo wrote:
> El 19/6/21 a las 15:22, Eric Pozharski escribió:
>> with <saj8m6$unh$1...@gioia.aioe.org> gamo wrote:
>>> El 18/6/21 a las 14:38, Eric Pozharski escribió:

>>>> What this task requires are: first, parser; then algebra will
>>>> emerge naturally. Instead you merge parser and algebra together
>>>> and press them against scalar
>>> Yes, there are parsers in symbolic math modules that could produce
>>> trees. But AFAIK there isn't the option to clear a variable, so I
>>> guess that that modules could be bridged to do the task. Or telling
>>> the story otherwise, I don't know how that trees could simplify the
>>> algorithm.
>> You're underestimating yourself. Of course you don't see algebra
>> emerging, but solely because you don't have the tree yet. Also, it
>> should be mentioned clearly -- that's you who will do all footwork,
>> but that's curse of the conduct.
> Yes, you might be right. In particular, I don't sense the need for
> parsing depth parentesees structures, but all these complicated work
> is already done and you have to not to reinvent the wheel.

Correct! But "work already done" are modules and, most probably,
classes. And this contradicts the conduct. Thus we arrive at the
show-stopper, aka -- Cost of Opportunity.

I suggest to push this somewhat deeper on a todo list, doing something
else, and call it a night.

gamo

unread,
Jun 20, 2021, 6:52:41 PMJun 20
to
El 20/6/21 a las 14:08, Eric Pozharski escribió:
>> Yes, you might be right. In particular, I don't sense the need for
>> parsing depth parentesees structures, but all these complicated work
>> is already done and you have to not to reinvent the wheel.
>
> Correct! But "work already done" are modules and, most probably,
> classes. And this contradicts the conduct. Thus we arrive at the
> show-stopper, aka -- Cost of Opportunity.
>
> I suggest to push this somewhat deeper on a todo list, doing something
> else, and call it a night.
>

With simple test a lot of concerns arise:

#!/usr/bin/perl -w

use strict;
use Math::Symbolic qw/parse_from_string/;
use Data::Printer;

my $tree = parse_from_string('m * c^2'); # maybe bc, gp, etc.
# but not perlish

# Second problem is that I cannot put "E = " beacuse the thing
# doesn't slurp equations, only expressions

p ($tree);

exit 1;

-----------------------

Result:
m * (c ^ 2) (Math::Symbolic::Operator)

The result is good because it detects that * is the main pivot operator.

I have to reconstruct the mental problem to adapt to this particular
structure. Don't know it it fits, and I'm scared about parsing parenthesees.



--
http://gamo.sdf-eu.org/
perl -E 'say "Something's wrong. I work too much for being economist."'

Eric Pozharski

unread,
Jun 21, 2021, 1:33:43 PMJun 21
to
with <saogrh$mjn$1...@gioia.aioe.org> gamo wrote:
> El 20/6/21 a las 14:08, Eric Pozharski escribió:

>>> Yes, you might be right. In particular, I don't sense the need for
>>> parsing depth parentesees structures, but all these complicated work
>>> is already done and you have to not to reinvent the wheel.
*SKIP*
>> I suggest to push this somewhat deeper on a todo list, doing
>> something else, and call it a night.

I still suggest pushing!

> With simple test a lot of concerns arise:
*SKIP*
> Result:
> m * (c ^ 2) (Math::Symbolic::Operator)

Indeed, doesn't look like a tree to me either :/ I suspect 'use
overload' in action.

> The result is good because it detects that * is the main pivot
> operator.

As of Math::Symbolic and friends. I suppose, M::S deals with function
definitions (as 'right side of equals') that's why 'equals' are not
acceptable. While you need 'problem solver'.

Indeed, there is that Math::SymbolicX::Calculator thing. Alas,
documentation consists of boilerplate (with elipsis), no examples, and
not my definition of the test coverage. IOW, it's not clear what it's
capable of, what are limits, and even if it works at all.

> I have to reconstruct the mental problem to adapt to this particular
> structure. Don't know it it fits, and I'm scared about parsing
> parenthesees.

Parentheses would pose a challenge (more like increase of complexity
than challenge), but it's doable.
Reply all
Reply to author
Forward
0 new messages