Jump to content
• Sign Up

# Are strings scalar or non-scalar?

• Scalar
0
• Non-Scalar
0

## Recommended Posts

In the book Introduction to Computation and Programming Using Python, Professor Guttag asserts that scalar objects are objects that are indivisible and lists int, float, bool, and None. He describes non-scalar objects as objects that have internal structure such as strings. This makes sense to me since a string is, in a sense, an array of characters. I was digging a little deeper and stumbled across an article that defines a scalar object as "an object that can have a single value" and includes both strings and complex numbers. This also makes sense to me if you follow this definition, so which is it?

Edited by mjbarr

#### Share this post

##### Share on other sites
45 minutes ago, mjbarr said:

In the book Introduction to Computation and Programming Using Python, Professor Guttag asserts that scalar objects are objects that are indivisible and lists int, float, bool, and None.

This is different than the normal definition of a scalar in science.  A Vector has magnitude and direction, a Scalar has magnitude only.

Edited by Bufofrog

#### Share this post

##### Share on other sites
2 hours ago, mjbarr said:

In the book Introduction to Computation and Programming Using Python, Professor Guttag asserts that scalar objects are objects that are indivisible and lists int, float, bool, and None. He describes non-scalar objects as objects that have internal structure such as strings. This makes sense to me since a string is, in a sense, an array of characters. I was digging a little deeper and stumbled across an article that defines a scalar object as "an object that can have a single value" and includes both strings and complex numbers. This also makes sense to me if you follow this definition, so which is it?

2 hours ago, Bufofrog said:

This is different than the normal definition of a scalar in science.  A Vector has magnitude and direction, a Scalar has magnitude only.

Computing science has its own alternative dfinitions for many things including vector.

I hadn't heard the one about a scalar but the indivisibility part makes sense since it precludes complex numbers, quaternions etc, which can be dissiciated into real and imaginary parts.

However my only Discrete Maths text to define scalar say that it is simply a real number (which of course has a special meaning in computing) .

Concrete or Discrete Mathematics is the correct topic home for this issue.

@bufofrog

On the matter of scalar - vector -tensor classification a tensor can be said to have a magnitude and two (or more) directions. But a tensor is also a vector as defined in a Linear Analysis vector space.

Edited by studiot

#### Share this post

##### Share on other sites
2 hours ago, mjbarr said:

In the book Introduction to Computation and Programming Using Python, Professor Guttag asserts that scalar objects are objects that are indivisible and lists int, float, bool, and None. He describes non-scalar objects as objects that have internal structure such as strings. This makes sense to me since a string is, in a sense, an array of characters. I was digging a little deeper and stumbled across an article that defines a scalar object as "an object that can have a single value" and includes both strings and complex numbers. This also makes sense to me if you follow this definition, so which is it?

Th answer depends on how you define 'scalar', which in turn depends on the programming language. And, sometimes, the context. I don't think there is an official definition in Python for what constitutes a scalar.

There are some languages where the difference is explicit in the language. For example, C has scalars and a few structured types (e.g. arrays and structs) but no in-built support for strings. So it is easy to say what is a scalar and what isn't. Similarly, Perl explicitly names scalar and non-scalar variables differently (\$var vs @var).

Languages like Pythin, that have strings as a fundamental type, may teat them as either scalars or non-scalars or, as int he case of Python. A string, in Python, is basically just a List of chars, so in that sense is a non-scalar type. But strings can be manipulated as single, atomic objects, so in that sense they are scalars.

So you need to add two more options to your list: Neither and Both.

#### Share this post

##### Share on other sites

Interesting question, I had to think for a while before answering.
@Strange and @studiot already covered two important aspects, I'll try to add a few comments from my background in the area.
In my more formal studies of computer science the definitions used by scientists were more formal and close related to Discrete Mathematics. A scalar was typically discussed an array of length one, or a 1x1 matrix. But when moving over to for instance software engineering the terms and definitions was/is affected by the sometimes less formal approach in programming environments or languages under consideration. Sometimes a "String" would mean an immutable sequence of a characters, referenced by some pointer. The fact that the pointer referenced one single value made it a scalar. This was also influenced by the fact that the String itself was immutable. (Java* and Python are two languages I think of).
In other environments the term "String" was an array of length n (or a 1 X n matrix) containing characters. In that case a string of characters was not matching the definitions of a scalar.

So I agree, depending on context I could select any of Yes, No, Both, Neither. But In python and related languages and situations, I think I would vote for String=Scalar if I had to choose one without knowing more about the context. That choice would be based more on experience and work rather than formal definitions from computer science / Discrete Mathematics.

#### Share this post

##### Share on other sites
On 12/30/2019 at 5:55 PM, Ghideon said:

This was also influenced by the fact that the String itself was immutable. (Java* and Python are two languages I think of).

.NET Framework String class implementation:

"The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system."

#### Share this post

##### Share on other sites
On 1/2/2020 at 3:07 PM, Sensei said:

.NET Framework String class implementation:

Thanks! I did not know that. I’ve not done much in .NET and haven’t looked at the internals. I did some quick reading to refresh memory and I'll try a slightly more generalized comment:

-When characters strings in a programming language are immutable, typically internally implemented using the flyweight* pattern, then string is a scalar type in that programming language.

Examples: Strings are scalars in .NET, Python and Java.
Example: A string is not a scalar in C. A string is an array of characters terminated by '\0' (null character)

Note: There might be exceptions to my statement and there might be other designs than flyweight that would classify strings as scalars.

*) References:

Flyweight is one of the design patterns documented in the Gang Of Four book: https://archive.org/details/designpatternsel00gamm/page/195

Early example from Stanford, flyweight objects in user interfaces: https://www.researchgate.net/profile/Mark_Linton2/publication/220877079_Glyphs_flyweight_objects_for_user_interfaces/links/58adbb6345851503be91e1dc/Glyphs-flyweight-objects-for-user-interfaces.pdf

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

×

• #### Activity

• Leaderboard
×
• Create New...

## Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.