Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

apply function and operator

37 views
Skip to first unread message

asetof...@gmail.com

unread,
Oct 31, 2015, 3:39:05 AM10/31/15
to
is it possible define operator% as
m<t> operator%(m<t>& a, t (f)(t))
where the 2th arg of operator% is one function
f:t->t?
or
m<t> operator%(m<t> a, t operator f(t))
when the function for apply in one
unary operator f:t->t

asetof...@gmail.com

unread,
Oct 31, 2015, 3:51:35 AM10/31/15
to
is it all ok in
a=b*c+k
where
m<t> a,b,c,k;
m<t> operator+(const m<t>& x, const m<t>& y)
{m<t> result;
....
return result;
}

m<t> operator*(const m<t>& x, const m<t>& y)
{m<t> result;
....
return result;
}

if one consider memory allocation
Thank you

asetof...@gmail.com

unread,
Oct 31, 2015, 4:57:52 AM10/31/15
to
is possible one operator% as this
<template Q> m<Q> operator%(m<t> a, Q operator f(t))
where f is a function f:t->Q
one operator that has result one matrix
of one other type in template

asetof...@gmail.com

unread,
Oct 31, 2015, 6:15:16 AM10/31/15
to
Above i mean
template <class q>
m<q> operator%(m<t> x, q (f)(t) )

Where f is a function t->q
In practice
get one matrix of element type t [x]
Create a matrix of element type q
has the same number elements
Matrix x and the same sizes
Apply to each element of x
f and fill the matrix result
Is possible in that way?
y=x%f


asetof...@gmail.com

unread,
Oct 31, 2015, 6:54:35 AM10/31/15
to
template <class q>
m<q> operator%(m<t> x, q (f)(t) )


If f is one operator for me that
is not possible in C++
m<u32> y(2,3), x(2,3);

y=x%~;
Seem ugly
possibly better
y=~%x;

template <class q>
m<q> operator%( q (f)(t), m<t> a)

with f operator or function...
but in the case of operator
=~%
sound strange...




Paavo Helde

unread,
Oct 31, 2015, 12:47:36 PM10/31/15
to
asetof...@gmail.com wrote in news:593ee2f2-a0cb-44c4-9778-
5fa3f1...@googlegroups.com:
Yes this is possible (ssuming I have interpreted your mumbling
correctly):

#include <vector>
#include <cassert>
#include <cstdlib>

template<typename T>
std::vector<T> operator%(const std::vector<T>& v, T (*f)(T)) {
std::vector<T> result;
for(auto x: v) result.push_back((*f)(x));
return result;
}

int main() {
std::vector<int> v = {1, -2, 3, -4};
std::vector<int> w = v % std::abs;
assert(w[3]==4);
}

asetof...@gmail.com

unread,
Oct 31, 2015, 1:20:51 PM10/31/15
to
Thank you
I wrote until now only something as
u32 f(u32 x){return 2*x;}

M<u32> x(2,3);
x.i(); x.apply(f);

asetof...@gmail.com

unread,
Oct 31, 2015, 2:18:56 PM10/31/15
to
I wrote:
u32 f(u32 x){return 2*x;}

M<u32> x(2,3);
x.i(); x.apply(f);
-------
I have seen in matlab anonymous
functions something as
x.apply( @(z) 2*z);
are there anonymous function in C++?

Wouter van Ooijen

unread,
Oct 31, 2015, 5:04:43 PM10/31/15
to
Op 31-Oct-15 om 7:18 PM schreef asetof...@gmail.com:
google "c++ lambda"

Wouter

asetof...@gmail.com

unread,
Nov 1, 2015, 4:14:12 AM11/1/15
to
Wouter wrote:
google "c++ lambda"

Wouter
-------
I find in internet some example
of anonymous function in C++
In our example will be something as
x.apply( u32 (u32 z)[](2*z) )
or something as above:
But I think the matlab way is better
x.apply( @(z) 2*z )
Because is more short:
Will be the compiler find the right
function:
if there are 2 or more functions
that are ok
than not compile and add type in
function
For me the best would be
x.apply(2*α)
Where α is a variable...
if one has 2 variables just write
2*α+β
as
(x,y)->(2*x+y)

asetof...@gmail.com

unread,
Nov 1, 2015, 4:17:06 AM11/1/15
to
First 10 Greek letters lowercase
as argument for anonymous function
0 new messages